Sunday, October 8, 2017

Sample program to sort an array of object by custom predefined sort order using JavaScript

Assume we have an object and we want to sort it based on custom predefined sort order value. We can utilize the JavaScript sort method to achieve this.
var items = [
    { firstname: 'Lazslo', lastname: 'Jamf',  accountType: 'Gold'},
    { firstname: 'Barack', lastname: 'Obamo',  accountType: 'Platinum' },
    { firstname: 'Pig',    lastname: 'Bodine',  accountType: 'Silver'},
    { firstname: 'Pirate', lastname: 'Prentice',  accountType: 'Platinum' }
];

Here, items is an object we need to sort by it's property 'accountType' and the sortOrder array is the predefined order it should follow. The order will be, first Platinum then Gold and then Silver.

Solution:
items.sort(function(a, b) {
    return sortOrder.indexOf(a.accountType) - sortOrder.indexOf(b.accountType);
});

So the final result will be,

var items = [
    { firstname: 'Barack', lastname: 'Obamo',  accountType: 'Platinum' },
    { firstname: 'Pirate', lastname: 'Prentice',  accountType: 'Platinum' }
    { firstname: 'Lazslo', lastname: 'Jamf',  accountType: 'Gold'},
    { firstname: 'Pig',    lastname: 'Bodine',  accountType: 'Silver'}
];

Sample program to sort an array of objects by its property value using JavaScript

Assume we have an object and we want to sort it based on it's property. We can utilize the JavaScript sort method to achieve this. The property value can be string or integer.

var items = [
    { firstname: 'Lazslo', lastname: 'Jamf',  accountType: 'Gold', age: 25},
    { firstname: 'Barack', lastname: 'Obamo',  accountType: 'Platinum', age: 45 },
    { firstname: 'Pig',    lastname: 'Bodine',  accountType: 'Silver', age: 36},
    { firstname: 'Pirate', lastname: 'Prentice',  accountType: 'Platinum', age: 29 }
];

Using the below function it it will be sorted by the 'accountType' string property.

items.sort(function(a, b) {
    var x = a.accountType.toLowerCase(), y = b.accountType.toLowerCase();
    return x < y ? -1 : x > y ? 1 : 0;
});

If we want to sort by the integer property 'age' means use the below method,


items.sort(function(a, b) {
     return a.age - b.age;
});

Monday, May 22, 2017

Sample SQL Server function to Strip HTML tags from a HTML string

If you want to remove the HTML tags from a HTML string and retrieve only plain text, the below SQL Server function can be used. It is just removing all the HTML tags by identifying '<' and '>'.

SQL Server Function :
create function [dbo].[StripHTML] 
(
 @HTMLText varchar(max)
)
returns varchar(max) 
as begin
 declare @Start int
    declare @end int
    declare @Length int
    set @Start = charindex('<',@HTMLText)
    set @end = charindex('>',@HTMLText,charindex('<',@HTMLText))
    set @Length = (@end - @Start) + 1
    while @Start > 0 and @end > 0 and @Length > 0
    begin
        set @HTMLText = stuff(@HTMLText,@Start,@Length,'')
        set @Start = charindex('<',@HTMLText)
        set @end = charindex('>',@HTMLText,charindex('<',@HTMLText))
        set @Length = (@end - @Start) + 1
    end
    return ltrim(rtrim(@HTMLText))
end

Sample Input:

select dbo.StripHTML('
<!DOCTYPE html><html><body><h1>My First Heading. </h1><p>My first paragraph.</p></body></html>
')

Output:

My First Heading. My first paragraph.