1

In a JSON array, each object has a key called popularity whose value is integer with no quotes.
There are also very few objects in the array which have value as undefined for this popularity key.
How do you sort the array, using JavaScript (ECMAScript 2015) preferred, by popularity key descending largest to smallest, skip or move the undefined ones to the bottom.

The following is not working

objGroup = objGroup.sort((a,b) => b.popularity - a.popularity ); // or
objGroup = objGroup.sort((a,b) => parseInt(b.popularity) - parseInt(b.popularity) );

EDIT

found
From MDN:

In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array

From the spec, 15.4.4.11 :

Because non-existent property values always compare greater than undefined property values, and undefined always compares greater than any other value, undefined property values always sort to the end of the result, followed by non-existent property values.

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
Ridhwaan Shakeel
  • 981
  • 1
  • 20
  • 39
  • 1
    can you share an example array? –  Jun 02 '21 at 11:54
  • 2
    You’re comparing `b` with itself instead of `a` with `b`. `-` coerces both operands to numbers, so `parseInt` is superfluous. Even if it wasn’t, please use `parseInt` [_with_ the second parameter, `10`](/q/16880327/4642212). Consider using [`Number`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#Function_syntax) or [`parseFloat`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) instead. – Sebastian Simon Jun 02 '21 at 11:55
  • 2
    There's no such thing as a _"json array"_, _"json object"_ or _"json key"_ ([There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/)). `objGroup` is an array of objects. – Andreas Jun 02 '21 at 11:56
  • 1
    _“\[T\]here are also very few \[…\] objects in the array which have value as `undefined` for this `popularity` key”_ — And what do you want to do with those? – Sebastian Simon Jun 02 '21 at 11:57
  • Does this answer your question? [Javascript: Sort object array by number properties which include undefined](https://stackoverflow.com/questions/56312968/javascript-sort-object-array-by-number-properties-which-include-undefined) – Heretic Monkey Jun 02 '21 at 12:02

2 Answers2

3

You could sort based on whether the popularity is undefined or not. Then sort based on the popularity

const input = [{ popularity: 2 }, { popularity: undefined }, { popularity: 1 }],
      isUndefined = o => typeof o.popularity === 'undefined'
      
input.sort((a, b) =>
  isUndefined(a) - isUndefined(b) || (b.popularity - a.popularity)
)

console.log(input)
adiga
  • 34,372
  • 9
  • 61
  • 83
3

In modern JS you can use ?? to return some value that is guaranteed to be sorted last if the property is missing, in an older version it can be emulated using a function.

objGroup = [
    {popularity: 1},
    {a:1},
    {popularity: 9},
    {popularity: 2},
    {b:1, popularity: null},
    {popularity: 8},
    {popularity: 3},
    {popularity: 7},
    {c:1},
    {d:1, popularity: undefined},
]

function coalesce(val, def) { 
  if (val === null || typeof val === 'undefined')
    return def
  return val
}

objGroup = objGroup.sort(function (a, b) {
    return coalesce(b.popularity, -Infinity) - coalesce(a.popularity, -Infinity)
})

console.log(objGroup)

The quote you're referring to applies to array elements themselves, not to their properties. Example:

a = [
    1,
    void 0,
    9,
    ,,,
    2,
    void 0,
    8
]

console.log(a.sort())
// [1, 2, 8, 9, undefined, undefined, empty × 3]
georg
  • 211,518
  • 52
  • 313
  • 390