0

I am getting a string an array of objects field from the data base which contains a value like:dataarray=[{name:'xyz',id:' '},{name:'abc',id:'DOC-TEMP-1'},{name:'efg',id:''},{name:'abc',id:'DOC-TEMP-21'},{name:'abc',id:''},{name:'jklm',id:'DOC-TEMP-2'}];.

I want to sort this array based on id so i tried to do it using sort method like this:

dataarray.sort(function(a,b){
                var a1 = a['id'].split('-');
                var b1 = b['id'].split('-');
                console.log('value in a1: '+a1);
                console.log('value in b1: '+b1);
                if(a1[2] =='' || a1[2]==null)
                {
                    a1[2] == 0;
                }if(b1[2]=='' || b1[2]==null)
                {
                    b1[2] == 0;
                }
                return a1[2]-b1[2];
            });

But it gives me uncorrect result.

what i want is it should first show the null ids then the id sort by number .

How can i do this ?

0

1 Answer 1

0

Just a small mistake, you are using '==' instead of '=' to assign values inside your conditionals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.