-2

I have an array of objects with one of the key's being price I am trying to sort the array of objects using .sort() to sort the objects by price, low to high.

function sortByPrice() {
  console.log(window.books)
  const sortedArray = window.books.sort((priceA, priceB) => {priceA.price - priceB.price})
  return sortedArray
};

I've checked the window.books to make sure that the array is correct and that priceA.price is a number value

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
stotheb
  • 11
  • 1. You should always tag your question with the programming language you use. 2. What is the issue/question? – UnholySheep May 24 '22 at 19:29
  • Please read the documentation on it https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort, and also https://stackoverflow.com/help/how-to-ask – ethry May 24 '22 at 19:37
  • 1
    thanks for your feedback, i'm new to tech and this platform is also new to me. – stotheb May 24 '22 at 20:21
  • I am using Javascript. My question/problem is the array that is returned is not sorted, so it ends up being the same as the array before the .sort(). How can i get the .sort() function to sort the array of objects by the value of the key: "price", which is a number – stotheb May 24 '22 at 20:24
  • I think what you are trying to do is sort an array of objects. It would be more clear if you provided a sample array in question, and as the other people mentioned, please provide a clear question. That being said, it looks like you aren't using the "sort" correctly. As @ethry alluded to, there is an example in the docs of exactly what you are trying to do. Give that a read – jbambrough May 24 '22 at 21:15
  • Btw, this is pretty much the exact same question posted here: https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values – jbambrough May 24 '22 at 21:48

2 Answers2

1

From JS doc

const items = [
  { name: 'Edward', value: 21 },
  { name: 'Sharpe', value: 37 },
  { name: 'And', value: 45 },
  { name: 'The', value: -12 },
  { name: 'Magnetic', value: 13 },
  { name: 'Zeros', value: 37 }
];

// sort by value
items.sort(function (a, b) {
  return a.value - b.value;
});

You're missing a return statement in your sort func.

Cathy Oun
  • 315
  • 2
  • 11
0

It would be helpful to see your window array of objs, but my guess is you're not iterating over the obj before using sort. Have you logged the values inside the sort function?

I suggest using the ...spread operator to iterate over the object and then you can apply .map and grab each price. also this wont modify your original object

const window = [
    { book: 'Book1', price: 15},
    { book: 'Book2', price: 9},
    { book: 'Book3', price: 25},
    { book: 'Book4', price: 3},
    { book: 'Book5', price: 13},
    ]
    
let sortedBooks = [...window.sort((a, b) => a.price - b.price)]
    
console.log(sortedBooks)

// expected:
// [
//     { book: 'Book4', price: 3 },
//     { book: 'Book2', price: 9 },
//     { book: 'Book5', price: 13 },
//     { book: 'Book1', price: 15 },
//     { book: 'Book3', price: 25 }
// ]