1

I'm trying to take a list of cars and sort them alphabetically.

So I found this method to sort:

let cars = this.state.cars;

cars.sort(function (a, b) {
    if (a.carName < b.carName) { return -1; }
    if (a.carName > b.carName) { return 1; }
    return 0;
})
console.log("items after sort: ", cars);

But when I write it out to the console, you can see the results below and they are not alphabetically sorted.

What could I be doing wrong?

Thanks!

items after sort: 
List {size: 431, _origin: 0, _level: 5, _root: VNode, …}
size: 431
__altered: true
_level: 5
_origin: 0
_root: VNode
array: Array(4)
0: VNode {array: Array(32)}
    0: {id: 1, carName: "Ford", isLocal: false, …}
    1: {id: 2, carName: "BMW", isLocal: true, …}
    2: {id: 3, carName: "Audi", isLocal: false,…}
    3: {id: 4, carName: "Toyota", isLocal: false,…}

1: VNode {array: Array(32)}
2: VNode {array: Array(32)}
3: VNode {array: Array(32)}
SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • I believe there should be an answer about this check: https://stackoverflow.com/questions/51165/how-to-sort-strings-in-javascript/51169 – halilcakar Feb 03 '21 at 19:46
  • @halilcakar that is actually the same Stack Overflow question I found my solution that doesn't work – SkyeBoniwell Feb 03 '21 at 19:47
  • I [can't reproduce](https://jsfiddle.net/40Lrwpdv/) your issue ...? – Teemu Feb 03 '21 at 19:49
  • try this it should work ``cars.sort( (a,b) => a.carName > b.carName ? 1 : -1)`` – Moufeed Juboqji Feb 03 '21 at 19:49
  • @Teemu I think your list of cars is set up more simply than mine. I think my list is different in that it has something called a VNode that contains the array. – SkyeBoniwell Feb 03 '21 at 19:59
  • 1
    @SkyeBoniwell That's the information you've provided. If you've sorted a different array, then you have found your mistake ... Notice, that in the code example you've `let cars = this.state.cars`, there's no `state` nor `cars` in the logged content. – Teemu Feb 03 '21 at 20:00
  • @Teemu yeah I don't know why either, I see no errors though, so doesn't that mean it's finding the cars to sort? Like if `cars.carName` did not exist would it not give me an error? Thanks! – SkyeBoniwell Feb 03 '21 at 20:06
  • 1
    No, it won't throw an error, it returns `undefined`. – Teemu Feb 03 '21 at 20:20

2 Answers2

2

Your log seems to imply that this data is from immutable.js.

By definition the data structure is .. immutable (cannot be modified). So running sort on it will not modify the existing data but will return a sorted copy.

So try something like

let cars = this.state.cars;

const sortedCars = cars.sort(function (a, b) {
    if (a.carName < b.carName) { return -1; }
    if (a.carName > b.carName) { return 1; }
    return 0;
})
console.log("items after sort: ", sortedCars);

or you could convert to normal js data structures with .toJS() and then do what you want.

let cars = this.state.cars.toJS();
cars.sort(function (a, b) {
    if (a.carName < b.carName) { return -1; }
    if (a.carName > b.carName) { return 1; }
    return 0;
})
console.log("items after sort: ", cars);
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0

Shortest possible code with ES6!

users.sort((a, b) => a. carName.localeCompare(b. carName))

String.prototype.localeCompare() basic support is universal!