-6

I have an array of objects:

array = [
  {
    name: 'fever',
    possibility: '20%',
  },
  {
    name: 'hiv',
    possibility: '25%',
  },
  {
    name: 'heart-attack',
    possibility: '20%'
  },
  {
    name: 'covid',
    possibility: '40%',
  },
]

I want to sort out the array of objects using its possibility. The object with higher possibility will be on top and if two or many objects have the same possibility then it will be sorted alphabetically. How can I do that?

Ori Drori
  • 183,571
  • 29
  • 224
  • 209
Khan Asfi Reza
  • 566
  • 5
  • 28
  • 1
    check this answer - write your own compare https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value – Jason Woods May 23 '20 at 12:40
  • 2
    There are a lot example in stackoverflow about sorting. You shoud try sorting by searching, if you can't still find solution than ask please – mr. pc_coder May 23 '20 at 12:43

6 Answers6

2

Use String.localeCompare() with the numeric option to sort using possibility, but use - (minus) to get descending results. If the comparison returns 0 (equals), use localeCompare again to compare the names:

const array = [{"name":"fever","possibility":"20%"},{"name":"hiv","possibility":"25%"},{"name":"heart-attack","possibility":"20%"},{"name":"covid","possibility":"40%"}]

const result = array.sort((a, b) => 
  -a.possibility.localeCompare(b.possibility, undefined, { numeric: true })
  ||
  a.name.localeCompare(b.name)
)

console.log(result)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
0

Maybe not only for this, but you could consider using the library Lodash.

It has a lot of cool features, such as sortBy that does exactly what you need.

_.sortBy(array, ['possibility']);

An additional note, is usually be better to store the possibility field (or any percentage value) with a number from 0 to 1, or at least from 1 to 100 as a number, and then adding the % char when displaying :)

To directly answer your questions, this works:

array.sort((e1, e2) => e1.possibility > e2.possibility ? 1 : -1)

Feel free to swap -1 and 1 to move from ascending to descending.

Balastrong
  • 4,336
  • 2
  • 12
  • 31
0

Try like this:

  var array = [
      {
        name: 'fever',
        possibility: '20',
      },
      {
        name: 'hiv',
        possibility: '25',
      },
      {
        name: 'heart-attack',
        possibility: '20'
      },
      {
        name: 'covid',
        possibility: '40',
      },
    ]


    function sortArray() {
      return array.sort((a, b)=> b.possibility- a.possibility)
    }

    console.log(sortArray());

Prints:

[{
  name: "covid",
  possibility: "40"
}, {
  name: "hiv",
  possibility: "25"
}, {
  name: "fever",
  possibility: "20"
}, {
  name: "heart-attack",
  possibility: "20"
}]

Based on answer from here: How to sort 2 dimensional array by column value?

JureW
  • 641
  • 1
  • 6
  • 15
0

Pure javascript solution would be :

array.sort((a,b) => a.possibility < b.possibility ? 1 : -1)

which would result in :

0: {name: "covid", possibility: "40%"}
1: {name: "hiv", possibility: "25%"}
2: {name: "heart-attack", possibility: "20%"}
3: {name: "fever", possibility: "20%"}
em_code
  • 379
  • 5
  • 17
0

const array = [
  {
    name: 'fever',
    possibility: '20%',
  },
  {
    name: 'hiv',
    possibility: '25%',
  },
  {
    name: 'heart-attack',
    possibility: '20%'
  },
  {
    name: 'covid',
    possibility: '40%',
  },
];

const sorted = array.sort((a, b) => {
  if (a.possibility > b.possibility) {
    return 1;
  }
  
  if (a.possibility < b.possibility) {
    return -1;
  }

  return 0;
});

console.log(sorted);
JSEvgeny
  • 2,550
  • 1
  • 24
  • 38
0

I've added another array entry just to double test the alphabetical ordering as a backup. Run the snippet and check the result:

let array = [
  {
    name: 'heart-attack',
    possibility: '20%',
  },
  {
    name: 'hiv',
    possibility: '25%',
  },
  {
    name: 'fever',
    possibility: '20%'
  },
  {
    name: 'covid',
    possibility: '40%',
  },
   {
    name: 'aaa',
    possibility: '40%',
  },
]

function compare( a, b ) {
  newA = Number(a.possibility.slice(0, -1))
  newB =  Number(b.possibility.slice(0, -1))
  if ( newA < newB ){
    return -1;
  } else if ( newA > newB ){
    return 1;
  } else if ( newA === newB && a.name < b.name){
    return -1;
  } else if ( newA === newB && a.name > b.name){
    return 1;
  }
  return 0;
}

const sortedArr = array.sort(compare)

console.log( sortedArr )
Ludolfyn
  • 1,806
  • 14
  • 20