0

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'
  }
}
Maddie
  • 229
  • 1
  • 10

2 Answers2

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);
The KNVB
  • 3,588
  • 3
  • 29
  • 54
-1

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>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49