3

I am interested in how you would sort the following data most effectively with jQuery:

There is a json object which I retrieve from the server:

var data = {/*json-object*/};
$.each(data.level1.sub1, function(){
    if(this.name.length < 2) {
        this.type = "banana";
        itemarray.push(this);
    }
    else {
        this.type = "apple";
        itemarray.push(this);
    }
});

$.each(data.level2.sub1, function(){
    this.type = "kiwi";
    itemarray.push(this);
});

This brings me to the point that I can list what's in the array (which is typeof object, not a "real" array) by doing this:

if (itemarray.length){
    $.each(itemarray, function(index, value){
      $('#wrapper').append('<li>' + index + ' : ' + value.name + ' * ' + value.type + '</li>');
    });
}  

Result:

0 : abc * apple
1 : a * banana
2 : lala * apple
3 : bcd - Copy * kiwi
4 : okl * banana

Now I would like to sort the array (which is typeof object, again): group by type and sort asc per type. How would you suggest doing that?

0

2 Answers 2

1

Since you're using the itemarray.push method, I assume that itemarray is a true array. Array objects (note: typeof [] === "object") support a sort() method.

Use the native Array.sort method, in conjunction with a function, as shown below. This method directly modifies the array.

var itemarray = [{type:"C"},{type: "B"}]; //Your array looks like this

//ASC result: [{type:"C"}, {type:"B"}]
itemarray.sort(function(x,y){
    return x.type.localeCompare(y.type);
});
//DESC result: [{type:"B"}, {type: "B"}]
itemarray.sort(function(x,y){
    return y.type.localeCompare(x.type);
});
Sign up to request clarification or add additional context in comments.

2 Comments

hey rob, thanks a lot so far. your sorting either brings me to: 0 : aname * apple 1 : bname * apple 2 : cname * apple 3 : aname * banana 4 : bname * banana 2 : aname * kiwi 3 : bname * kiwi when i process the first sorting method twice (x.type.localeCompare(y.type) not the other way round), thats nice, because it sorts types and names each. BUT i actually would like to have the order: apple, kiwi, banana! I tried out a few combinations, but sort() sorts the types-names themselves - so how can i get to this?
ok, got it: to group the type-groups in the desired order, i do the following after sorting: applearray = $.grep(itemarray, function(n, i){ return (n.type==="apple"); }); bananaarray = $.grep(itemarray, function(n, i){ return (n.type==="banana"); }); kiwiarray = $.grep(itemarray, function(n, i){ return (n.type==="kiwi"); }); itemarray=applearray.concat(kiwiarray).concat(bananarray);
0

use it this way

$(document).ready(function () {
  var data = {/*json-object*/};      
         ;
  data = data.sort(function (a, b) {
    if (a.type < b.type) { return -1 };
    if (a.type > b.type) { return 1 };
    return 0;
  });
  $.each(data, function (index, value) {
    $("#wrapper").append("<li>" + value.index + " : " + value.name + " * " + value.type + "</li>");
  });
});

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.