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

234
Views
object not sort based on the value in JavaScript (having key and value number)

Javascript expected object result not getting when sorting on value numbers( and both key and value are also numbers)

let team_id_Point = {
  26: 15,
  32: 1,
  127: 21,
};

function sortObjectbyValue(obj = {}, asc = true) {
  const ret = {};
  Object.keys(obj)
    .sort((a, b) => obj[asc ? a : b] - obj[asc ? b : a])
    .forEach((s) => (ret[s] = obj[s]));
  return JSON.stringify(ret);
}

console.log(sortObjectbyValue(team_id_Point, false));

result from above:

{"26":15,"32":1,"127":21}

required output:

{"127":21,"26":15,"32":1}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Although JavaScript object properties have an order now, relying on their order is an antipattern.

If you need order, use an array.

required output

You cannot get the required output with a JavaScript object, because:

  • Those property names are array index names, and so the object's "order" puts them in order numerically.
  • JSON.stringify follows the property order of the object.

Also note that JSON (as distinct from JavaScript) considers objects "...an unordered set of name/value pairs..." So to JSON, {"a": 1, "b": 2} and {"b": 2, "a": 1} are exactly equivalent.

You could use a Map, but then the JSON representation wouldn't be what you're expecting.

Or you could build the JSON manually (ignoring that JSON doesn't consider objets to have any order), using JSON.stringify for the values but outputting the {, property names, and } yourself.

But really, if you need order, use an array.

Here's an example using an array of arrays:

let team_id_Point = [
    [26, 15],
    [32, 1],
    [127, 21],
];

function sortTeamIdPoint(data = [], asc = true) {
  const result = [...data];
  const sign = asc ? 1 : -1;
  result.sort((a, b) => (a[0] - b[0]) * sign);
  return JSON.stringify(result);
}

console.log(sortTeamIdPoint(team_id_Point, false));

Or an array of objects:

let team_id_Point = [
    {id: 26, points: 15},
    {id: 32, points: 1},
    {id: 127, points: 21},
];

function sortTeamIdPoint(data = [], asc = true) {
  const result = [...data];
  const sign = asc ? 1 : -1;
  result.sort((a, b) => (a.id - b.id) * sign);
  return JSON.stringify(result);
}

console.log(sortTeamIdPoint(team_id_Point, false));

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!