Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Sort object by category and name

Is there a way in jQuery or javascript to sort the data by category in descending order and name in ascending order?

I have tried to sort the data twice similar to the sample code below, but it's not working as it should be.

The data should be listed by category like B, A and will sort next by name like AA, AB, BB and so on.

Sample data and sort script:

var data = {
  0: {
    category: 'A',
    name: 'AA'
  },
  1: {
    category: 'B',
    name: 'BB'
  },
  2: {
    category: 'A',
    name: 'AB'
  },
  3: {
    category: 'A',
    name: 'BB'
  }
}

var items = $( $.map( data, function ( val, i ) {
  return val;
} ) );

// Sort data by category in DESC order.
function sortByCategoryDescOrder( a, b ) {
  if ( a.category < b.category ) {
    return -1;
  }
  if ( a.category > b.category ) {
    return 1;
  }
  return 0;
}

function sortByNameAscOrder( a, b ) {
  if ( a.name < b.name ) {
    return -1;
  }
  if ( a.name > b.name ) {
    return 1;
  }
  return 0;
}

items.sort(sortByCategoryDescOrder);
items.sort(sortByNameAscOrder);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>

The expected output should be:

var data = {
  0: {
    category: 'B',
    name: 'BB'
  },
  1: {
    category: 'A',
    name: 'AA'
  },
  2: {
    category: 'A',
    name: 'AB'
  },
  3: {
    category: 'A',
    name: 'BB'
  }
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is my solution:

let data = {
  0: {
    category: 'A',
    name: 'AA'
  },
  1: {
    category: 'B',
    name: 'BB'
  },
  2: {
    category: 'A',
    name: 'AB'
  },
  3: {
    category: 'A',
    name: 'BB'
  }
}

let result=Object.values(data).sort((a,b)=>{
    if (a.category>b.category){
     return -1;
  } else {
    if (a.category<b.category){
         return 1;
    } else {
       if (a.name > b.name) {
         return 1
       } else {
          if (a.name < b.name) {
            return -1
          }
       }
    
    }
  }
});
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Two issues.

  • Your sortByCategoryDescOrder sort return values are incorrect. Descending order sort should return +1 when a < b, -1 when a > b and 0 on a === b.
  • sortByNameAscOrder function, shouldnot compare objects with different category. You have already sorted the array based on category, if you again sort the array by comparing different category value, it will give you the soeted format of original array. You should return 0 for different category values, which means you are not changing order in that case.

Working Fiddle

var data = {
    0: {
        category: 'A',
        name: 'AA'
    },
    1: {
        category: 'B',
        name: 'BB'
    },
    2: {
        category: 'A',
        name: 'AB'
    },
    3: {
        category: 'A',
        name: 'BB'
    }
}

var items = $($.map(data, function (val, i) {
    return val;
}));

// Sort data by category in DESC order.
function sortByCategoryDescOrder(a, b) {
    if (a.category < b.category) {
        return 1;
    }
    if (a.category > b.category) {
        return -1;
    }
    return 0;
}

function sortByNameAscOrder(a, b) {
    if(a.category !== b.category) {
        return 0;
    }
    if (a.name < b.name) {
        return -1;
    }
    if (a.name > b.name) {
        return 1;
    }
    return 0;
}

console.log(items.sort(sortByCategoryDescOrder));
console.log(items.sort(sortByNameAscOrder));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!