Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This question already has an answer here:

I have following JSON Object

   var data1 =  [
    {id: "1", name: "b", lastname: "y", marks: "10"},
    {id: "1", name: "a", lastname: "x", marks: "20"},
    {id: "2", name: "a", lastname: "x", marks: "30"},
    {id: "2", name: "b", lastname: "x", marks: "40"},
    {id: "2", name: "c", lastname: "z", marks: "60"},
    {id: "3", name: "d", lastname: "x", marks: "50"},
    {id: "3", name: "a", lastname: "c", marks: "70"}    
  ];

I what to sort this object based on different condition like-

first sort by name in asc order

than sort result by last name in desc order

than sort rsult by marks in desc order

fields and their order type is generated dynamically by web page.

*** here sort feilds and theire types are not fixed it may be anything like name asc, marks asc, lastname desc or marks desc, lastname desc, name asc

Can any one help me to suggest any jquery, java script plugin or function ?

share|improve this question

marked as duplicate by T.J. Crowder javascript Feb 20 '15 at 9:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4  
"I have following JSON Object" That's not JSON, that's JavaScript. – T.J. Crowder Feb 20 '15 at 9:26
1  
have you tried anything from your side? – Bhushan Kawadkar Feb 20 '15 at 9:27
    
@T.J.Crowder : In my case sort feilds and theire types are not fixed it may be anything like name asc, marks asc, lastname desc or marks desc, lastname desc, name asc Is your suggested answer working for this type condition – Anupam Sharma Feb 20 '15 at 10:01
    
@AnupamSharma: The fundamentals of sorting by multiple criteria are there; how you apply the criteria is just a matter of further code. If you run into trouble making the order variable in your code, I suggest searching (there's probably already an answer here about that) and if you don't find an answer, asking a question with an example showing what you've tried to get the sorting working, ideally in a functioning Stack Snippet (obviously the sorting won't be functional in the snippet -- or you wouldn't be asking! :-) ). – T.J. Crowder Feb 20 '15 at 10:52

You can use sort to achieve this if you implement you own custom sort function. Something like this:

function sortFunc(a, b) {
    if (a.name < b.name)  return -1;
    else if (a.name > b.name)  return 1;
    else {
        if (a.lastname < b.lastname)  return 1;
        else if (a.lastname > b.lastname)  return -1;
        else {
            if (a.marks < b.marks) return 1;
            else if (a.marks > b.marks) return -1;
        }
        return 0;
    }
}

console.log(data1.sort(sortFunc));

Example fiddle

share|improve this answer
    
Rory your answer is work for only fix pattern but In my case it is not fixed it may be name >> lastname >> marks or may be marks>> name>> last name or anything sorting Fields order and sorting type is decided by webpage – Anupam Sharma Feb 20 '15 at 10:00
    
That's not a very good idea. Property names should always be consistent. – Rory McCrossan Feb 20 '15 at 10:03
    
Rory property names are fixed but their position is not fixed like suppose I want to sort reasult based on name and than marks but other one wants to sort data based on marks and name – Anupam Sharma Feb 20 '15 at 10:05
    
Ah, I understand what you mean now. You would probably have to create an order function for each individual case and apply that as needed. – Rory McCrossan Feb 20 '15 at 10:07

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