Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.