Laurens Mäkel
Laurens Mäkel

Reputation: 815

Javascript - Sorting array with objects based on objects value

So I am trying to sort my array with objects based on an objects value..

var mostCheapHistory [ { lowestTitle: title goes here, lowestPrice: 100 }, another obj, another, another } ]

And I want to sort them based on their price, so I came up with this:

var historyObj = mostCheapHistory[0];

for(var y in mostCheapHistory){
    nextObj = mostCheapHistory[y];

    console.log('Is '+ historyObj.lowestPrice + ' more as ' + nextObj.lowestPrice + ' ?');
    console.log(historyObj.lowestPrice > nextObj.lowestPrice);
    console.log('-----')
}

And this is the output...

Is 124.98 more as 124.98 ?
false
-----
Is 124.98 more as 18.59 ?
false
-----
Is 124.98 more as 25.9 ?
false
-----
Is 124.98 more as 26.99 ?
false
-----
Is 124.98 more as 34.76 ?
false
-----

What the hell is going on? It's obvious that 124.98 is more as 34.76 and yet it gives false?

The array is made using this code:

for(var x=0; x < items.length; x++){
            var title = items[x].title[0];
            var price = items[x].sellingStatus[0].currentPrice[0].__value__;

            var item = 
                { 
                    lowestPrice : price,
                    lowestTitle : title
                }

            if(x === 0){
                mostCheapHistory.push(item);                
            }
            else{
                for(var y=0; y < mostCheapHistory.length; y++){
                    if(mostCheapHistory[y].lowestPrice > price ){
                        if(mostCheapHistory.length < 5){
                            mostCheapHistory.push(item);
                            break;
                        }
                        else{
                            mostCheapHistory[y] = item;
                            break;
                        }
                    }
                }
            }
        }

Upvotes: 1

Views: 1358

Answers (4)

AirNoir
AirNoir

Reputation: 251

I think the type of lowestPrice is string not float, use parseFloat(historyObj.lowestPrice) to compare the two values

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386816

It looks like, you are comparing strings instead of numerical values. If you say (in comments), that the sorting function

array.sort((a, b) =>  a.lowestPrice - b.lowestPrice);

solves your problem, then while this sort callback uses an implicit casting to number with the minus - operator.

The result is a sorted array with values as string.

Upvotes: 2

Djaouad
Djaouad

Reputation: 22794

Use the predefined sort function :

mostCheapHistory.sort(function(a,b){
    return a.lowestPrice - b.lowestPrice;
}

+ Your code might be pushing strings rather than numbers, which explains why 124.98 > 34.76 => true but '124.98' > '34.76' => false, since the string '34.76' is greater than '124.98' in string comparison.
Use parseFloat() for the prices then check again.

Upvotes: 2

user6624150
user6624150

Reputation:

You can sort these based on price using Array.prototype.sort() (See MDN documentation)

e.g.

var data = [
{
  title: "title1",
  value: 123
},
{
  title: "title2",
  value: 324
},
{
  title: "title3",
  value: 142
}];

var sorted = data.sort(function(a, b) {
  return a.value > b.value;
});

Upvotes: 0

Related Questions