skip to Main Content

I’m trying to sort an array of objects based on a property value, but the property is a string that includes a number. For example, I have an array of objects like this:

const data = [
 { name: 'Item 1', value: '2 apples' },
 { name: 'Item 2', value: '10 oranges' },
 { name: 'Item 3', value: '1 banana' },
 { name: 'Item 4', value: '5 strawberries' }
];

I want to sort the array by the numeric value in the value property, so that the sorted array looks like this:

[  { name: 'Item 3', value: '1 banana' },  { name: 'Item 1', value: '2 apples' },  { name: 'Item 4', value: '5 strawberries' },  { name: 'Item 2', value: '10 oranges' }]

Here is my code which is not working

data=data.sort((a,b) => b.value-a.value)

One solution can be: "split value on space then get number from 0th index and then compare". Is there any better way of sorting?

4

Answers


  1. You could use parseInt or parseFloat to convert the value into a number and then do the sorting algorithm:

    parseFloat('10 oranges'); // 10
    

    Note: this only works when the number always appears at the beginning of the string.

    const data = [
     { name: 'Item 1', value: '2 apples' },
     { name: 'Item 2', value: '10 oranges' },
     { name: 'Item 3', value: '1 banana' },
     { name: 'Item 4', value: '5 strawberries' }
    ];
    
    const output = data.sort((a, b) => parseFloat(a.value) - parseFloat(b.value));
    
    console.log(output);
    Login or Signup to reply.
  2. You can use replace and regex to extract numbers to sort:

    const data = [
        { name: 'Item 1', value: '2 apples' },
        { name: 'Item 2', value: '10 oranges' },
        { name: 'Item 3', value: '1 banana' },
        { name: 'Item 4', value: '5 strawberries' }
       ];
       
    
    const sorted = data.sort((a, b) => parseInt(a.value.replace(/D/g, "")) - parseInt(b.value.replace(/D/g,"")));
    console.log(sorted);
    Login or Signup to reply.
  3. The extractNumber function extracts the first number found in a string using a regular expression (/d+/) and then returns it as an integer using parseInt(). If no number is found, it returns 0. Then the function is used inside the sort method to compare the numbers.

    const data = [
      { name: 'Item 1', value: '2 apples' },
      { name: 'Item 2', value: '10 oranges' },
      { name: 'Item 3', value: '1 banana' },
      { name: 'Item 4', value: '5 strawberries' }
    ];
    
    const extractNumber = (str) => {
      const num = str.match(/d+/);
      return num ? parseInt(num[0]) : 0;
    };
    
    const sortedData = data.sort((a, b) => extractNumber(a.value) - extractNumber(b.value));
    
    console.log(sortedData);
    Login or Signup to reply.
  4. data.sort((a, b) => {
    const numA = parseInt(a.value);
    const numB = parseInt(b.value);
    return numA - numB;
    });
    

    Try with above logic, it should work

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search