Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:15:33+00:00 2026-06-16T00:15:33+00:00

In reference to this posting ; I can sort an Array of JSON Object

  • 0

In reference to this posting; I can sort an Array of JSON Object by 2 fields.
And this posting also states:

“To add other columns to sort on, you can add other items in the array comparison.”

//THIS WORKS BUT NEEDS TO SORT BY ALL 5 FIELDS..
function sortRecords(a, b){
//note the minus before -cmp, for descending order
// field1 (Y or N), field2 (numeric), field3 (numeric), 
// field4 (Y or N), field5 (Y or N)
return cmp( 
    [-cmp(a.field1, b.field1), cmp(a.field2, b.field2)], 
    [-cmp(b.field1, a.field1), cmp(b.field2, a.field2)]
 );}

//THIS ONLY SORTS BY field1 and field2
function sortRecords(a, b){
//note the minus before -cmp, for descending order
// field1 (Y or N), field2 (numeric), field3 (numeric), 
// field4 (Y or N), field5 (Y or N)
return cmp( 
    [-cmp(a.field1, b.field1), cmp(a.field2, b.field2), cmp(a.field3, b.field3)], 
    [-cmp(b.field1, a.field1), cmp(b.field2, a.field2), cmp(b.field3, a.field3)]
 );}

function cmp(x,y){
return x > y ? 1 : x < y ? -1 : 0;
}


///implementation
data = $(data).sort(sortRecords);

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:15:34+00:00Added an answer on June 16, 2026 at 12:15 am

    I have been using StackOverflow and I combined several examples of how to sort/order; and this function seems to give me what I am looking for…

    given this array of JSON objects as [data].

    BEFORE
    {“name”:”JOE”,”field1″:”Y”,”field2″:3,”field3″:79,”field4″:”Y”,”field5″:”Y”}
    {“name”:”BEN”,”field1″:”Y”,”field2″:15,”field3″:113,”field4″:”Y”,”field5″:”N”}
    {“name”:”SUE”,”field1″:”Y”,”field2″:35,”field3″:395,”field4″:”Y”,”field5″:”Y”}

    I can call sortRecords() to sort how I want.

    AFTER
    {“name”:”JOE”,”field1″:”Y”,”field2″:3,”field3″:79,”field4″:”Y”,”field5″:”Y”}
    {“name”:”SUE”,”field1″:”Y”,”field2″:35,”field3″:395,”field4″:”Y”,”field5″:”Y”}
    {“name”:”BEN”,”field1″:”Y”,”field2″:15,”field3″:113,”field4″:”Y”,”field5″:”N”}

    function sortRecords(a, b){
        if (a.field1 < b.field1){
            return 1;
        } else if (a.field1 > b.field1){
            return -1;
        } else if (a.field2 < b.field2){
            return 1;
        } else if (a.field2 > b.field2){
            return -1;
        } else if (a.field3 < b.field3){
            return 1;
        } else if (a.field3 > b.field3){
            return -1;
        } else if (a.field4 < b.field4){
            return -1;
        } else if (a.field4 > b.field4){
            return 1;
        } else if (a.field5 < b.field5){
            return -1;
        } else if (a.field5 > b.field5){
            return 1;
        } else {
            return 0;
        }
    }
    
    data = $(data).sort(sortRecords);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen another reference to this but I can't understand the response. I am
I'm really posting this question so that others searching for the answer can find
Reference: This is a self-answered question. It was meant to share the knowledge, Q&A
For reference this is in a C# MVC2 website. I'm looking to save a
Is it OK to reference this when initializing a field? public class MainClass {
In reference with this question We have a header with a date picked component
In reference to this answer to a Stack Overflow question : what is bench-testing
an svn:externals reference like this dojo -r 21434 http://svn.dojotoolkit.org/src/dojo/trunk/ is happily accepted by subversive
With the reference to this question:- JavaScript -Change CSS color for 5 seconds Working
I get a null - reference in this Code-line: **DataRow dr = tableSelectedItems.NewRow();** I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.