1

Trying to sort/reorder following objects as from lowest and highest value

{ 'xlg': '10',
  'lg': '9.6',
  'md': '9',
  'sm': '5',
  'xsm': '3',
  'xxsm': '2',
  'xxxsm': '1.75',
  'xxxxsm': '1'}

something like this :

{ 'xxxxsm': '1',
  'xxxsm': '1.75',
  'xxsm': '2',
  'xsm': '3',
  'sm': '5',
  'md': '9',
  'lg': '9.6',
  'xlg': '10'}

and here is my input so far :

const sortObjectByValues = object => {
    const sortedArray = Object.values(object).sort();
    const sortedObject = sortedArray.reduce((acc, cur) => {
       // No idea....
        return acc;
    }, {});

    return sortedObject;
};

I am sure I can workout with reduce function. anyone could help me this out please ?

thanks

Danny Kim
  • 809
  • 1
  • 7
  • 15
  • 1
    *"Sort by values using reduce function"* `reduce` is not the correct tool for sorting things. Why do you think you need to use it? – T.J. Crowder May 16 '20 at 11:08
  • 1
    Also note that although object properties do have order now, it's almost never a good idea to *use* that order. If you want order, use an array. – T.J. Crowder May 16 '20 at 11:09

3 Answers3

2

Rather than reduce, using sort to sort the object entries in numeric value order would be easier:

const obj = { 'xlg': '10',
  'lg': '9.6',
  'md': '9',
  'sm': '5',
  'xsm': '3',
  'xxsm': '2',
  'xxxsm': '1.75',
  'xxxxsm': '1'};
const result = Object.fromEntries(
  Object.entries(obj)
    .sort((a, b) => a[1] - b[1])
);
console.log(result);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Use object entries, sort them with Number values and build the object again.

Update: updated answer to handle when string has pre/post chars for number. (Basically extracting the number from string).

const obj = {
  xlg: "10",
  lg: "9.6",
  md: "9",
  sm: "5",
  xsm: "3",
  xxsm: "2",
  xxxsm: "1.75",
  xxxxsm: "1",
  abc: "abc2.05def",
  usd: "$3.15",
  xyz: "1.72em"
};

const extractNumber = ([,str]) => str.replace(/[a-zA-Z$]+/g, '');

const result = Object.fromEntries(
  Object.entries(obj).sort((a, b) => extractNumber(a) - extractNumber(b))
);

console.log(result);
Siva K V
  • 10,561
  • 2
  • 16
  • 29
  • Thanks - what if value is not a number but something like '1.75em' , '9.6em' . sorry mate – Danny Kim May 16 '20 at 11:20
  • @DannyKim, I missed that before, just updated the answer. First extract the number from string (above is one example of regex), on then use it in sort. This should work for both `1.75em` and like `$2.0` etc. Hope this helps. – Siva K V May 17 '20 at 17:21
0
Object.keys(t)
    .sort((a,b) => parseFloat(t[a]) - parseFloat(t[b]))
    .reduce((acc, key) => ({ ...acc, acc[key]:t[key] }), {});
kurumkan
  • 2,635
  • 3
  • 31
  • 55
  • 6
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – SherylHohman May 16 '20 at 21:06
  • You are absolutely right @SherylHohman – Ganesh Kumar Aug 07 '21 at 17:44