5

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 ?

4
  • 4
    "I have following JSON Object" That's not JSON, that's JavaScript. Commented Feb 20, 2015 at 9:26
  • 1
    have you tried anything from your side? Commented Feb 20, 2015 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 Commented Feb 20, 2015 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! :-) ). Commented Feb 20, 2015 at 10:52

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
That's not a very good idea. Property names should always be consistent.
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
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.