0

I have an array of objects similar to this :

 var this array = [
    { 
    "id" : "A20",
    "age" : "16"
    },
    {
    "id" : "A2",
    "age" : "12"
    },
    { 
    "id" : "B16",
    "age" : "45"
    }]

I need to sort this array by their ID, So it would end up like :

this array = [
        { 
        "id" : "A2",
        "age" : "12"
        },
        {
        "id" : "A20",
        "age" : "16"
        },
        { 
        "id" : "B16",
        "age" : "45"
        }]

I have this method of sorting :

nodes.sort(function(a,b){
    // console.log(a.Pos)
    // console.log(a.pinNum)
    var alc = a.id, blc = b.id;
    console.log(alc)
    return alc > blc ? 1 : alc < blc ? -1 : 0;
    });

But this only works alphabetically. I have also found this but it gives back just the strings I pass to the function not the array of objects :

function sortArray(arr) {
        var tempArr = [],
        n;
        console.log(arr)
        for (var i in arr) {

            var thisString = arr[i].id;

            // tempArr[i] = arr[i].match(/([^0-9]+)|([0-9]+)/g);
            tempArr[i] = thisString.match(/([^0-9]+)|([0-9]+)/g);
            for (var j in tempArr[i]) {
                if (!isNaN(n = parseInt(tempArr[i][j]))) {
                    tempArr[i][j] = n;
                }
            }
        }
        tempArr.sort(function (x, y) {
            for (var i in x) {
                if (y.length < i || x[i] < y[i]) {
                    return -1; // x is longer
                }
                if (x[i] > y[i]) {
                    return 1;
                }
            }
            return 0;
        });
        for (var i in tempArr) {
            arr[i] = tempArr[i].join('');
        }
        return arr;
    }
    console.log(sortArray(nodes).join(","));

What I need is the second sorting function but with the ability to sort both alphabetically and numerically. Thanks

7
  • var alc = +a.id, blc = +b.id; Commented Oct 23, 2015 at 19:27
  • @dandavis could you explain more on your point please ? Commented Oct 23, 2015 at 19:31
  • @dandavis but my string is made up of both letters and numbers ? "A20" ... Commented Oct 23, 2015 at 19:34
  • 1
    ooooooops, i should read it closer, sorry. Commented Oct 23, 2015 at 19:37
  • Tell us more about the rule of compare, is it simply remove any prefix non-digits? or there's some other rules? Commented Oct 23, 2015 at 19:38

3 Answers 3

2
var array = [{ 
   "id" : "A20",
   "age" : "16"
}, {
   "id" : "A2",
   "age" : "12"
}, { 
   "id" : "B16",
   "age" : "45"
}];

array.sort(function(a, b) {
 var aSplit = /(\D*)(\d*)/.exec(a.id),
     bSplit = /(\D*)(\d*)/.exec(b.id);

 if( aSplit[1] !== bSplit[1] )
    return aSplit[1].localeCompare(bSplit[1]);
 else
    return aSplit[2] - bSplit[2];
});
Sign up to request clarification or add additional context in comments.

10 Comments

nice answer but it doesn't work. If i have these as id's : 'A1','A20','A11','A'2. The output after sorting is : 'A1','A11','A2','A20'
error, if you change id 'A2', for 'A120', you receive a wrong order
You'll have to split then, compare the letters then the digits.
i thought as much :( Thanks for the input. Ill try do the split and answer the question, if you manage to solve it ill chose yours as the correct answer :)
it's "localeCompare" (with an 'e'), but it won't help with the mixed letter and number problem here...
|
0

Use function .sort in array, and pass has argument, a callback.

This callback receive elements to iterate, and you can compare values, if return true, switch order of elements, for i.e:

arr.sort(function(a, b) {
    return parseInt(a.age) > parseInt(b.age))
};

or, if you use ecma script 6, you can use arrow function, is better, i.e. below:

arr.sort((a, b) => parseInt(a.age) > parseInt(b.age))

Docs here: http://www.w3schools.com/jsref/jsref_sort.asp

1 Comment

it's not the age i want to sort, its the ID
0

I think you can use localeCompare() to compare strings. Try this code, it should work:

nodes.sort(function(a,b){
    // console.log(a.Pos)
    // console.log(a.pinNum)
    var alc = a.id, blc = b.id;
    console.log(alc)
    return alc.localeCompare(blc);
});

Comments

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.