Sorting array of numbers - javascript

I have some certain things which will be pushed into array like that (down below) and I would like to sort the item(s) by the item price. I tried using a sort but it doesn't work for me. After that I would like to loop that array and retrieve the item name and a item price.
Pushing item into array: array.push(itemName,itemPrice)
Trying to use sort function:
array.sort(function(a,b){
return b[1] - a[1]
});

EDIT: Agree with Nina's comment. changing compare function
Looks like you need an array of objects and define your own comparison function for sorting.
So you need to add element to array like this:
array.push({itemName:name,itemPrice:price})
Define custom comparison function
function compare(a,b) {
return a.itemPrice-b.itemPrice;
}
Use custom function
array.sort(compare)

I think you want to push the tuple (itemName, itemPrice) to array which can be done either way:
array.push([itemName, itemPrice])
or
array.push({ itemName: itemName, itemPrice: itemPrice })
// and instead of accessing a[1] or b[1], access a.itemPrice and b.itemPrice respectively.
array.push(x, y) simply appends two different elements x and y to the array

I think what you want to do is push objects not just values!
array.push( {
name: itemName,
price: itemPrice
} );
Which you can then sort using:
array.sort(function(a, b){
return b.price - a.price;
});

// Sort BY keys( and values).
int[] keys = { 4, 7, 2, 0 };
int[] values = { 1, 2, 3, 4 };
Array.Sort(keys, values);
foreach (int key in keys)
{
Console.Write(key);
Console.Write(' ');
}
Console.WriteLine();
foreach (int value in values)
{
Console.Write(value);
Console.Write(' ');
}
Console.WriteLine();
I hope this will help you ;)

here is a sample code, have fun !
var arr = [
{
"name": "Item1",
"price": 100,
},
{
"name": "Item2",
"price": 500,
},
{
"name": "Item3",
"price": 250,
},
{
"name": "Item4",
"price": 1000,
}
]
arr = arr.sort(function compare(a, b) {
if (a.price > b.price)
return 1;
if (a.price < b.price)
return -1;
return 0;
});
console.log(arr);

Related

Sort array with Objects inside of Objects

I have this array:
[
["name1", { count: 20 }],
["name2", { count: 10 }]
]
How would I go about sorting this array by the value of count?
I have tried using the sort function,
const sort = Array.sort((a, b) => b.count - a.count);
But this didn't change anything.
You need to access the second entry in the arrays inside the outer array. Your code is using count on the array entries, but they don't have a count property:
theArray.sort((a, b) => b[1].count - a[1].count);
Note also that you call sort on the actual array, not the Array constructor. It also sorts the array in-place, rather than returning a sorted array (it also returns the array you call it on, though).
Live Example:
const theArray = [
["name1", { count: 20 }],
["name2", { count: 10 }],
["name3", { count: 15 }]
];
console.log("before:", theArray);
theArray.sort((a, b) => b[1].count - a[1].count);
console.log("after:", theArray);
.as-console-wrapper {
max-height: 100% !important;
}

Filter highest numbers out of an object

I have the following object, with points per sport for an individual person. This information comes out of a database, based on the search for "Jack Miller"
Jdata = {
"name": "Jack Miller",
"sports": {
"Basketball": 2,
"Football": 3,
"Iceskating": 5,
"Running": 4,
}
}
I would like to display the top 2(3) sports for the name on my HTML page. Do to that, I was thinking to extract the information into an array like this:
SportVal = [];
SportNames = [];
for(var key in this.Jdata.sports){
if(!this.Jdata.sports.hasOwnProperty(key)){
continue;
}
this.SportVal.push(this.Jdata.scores[key]);
this.SportNames.push(key)
}
Then I would have to sort the SportVal array in descending order and can use e.g. ngFor in the HTML to display the results.
However, how would I get back the corresponding Names? Also I don't think this is the most effective way, since I run into a problem if scores are equal. So do you maybe have a better idea how to do this?
You could use Array.prototype.sort() to do it.
Jdata = {
"name": "Jack Miller",
"sports": {
"Basketball": 2,
"Football": 3,
"Iceskating": 5,
"Running": 4,
}
}
const sorted = Object.entries(Jdata.sports)
.sort((a, b) => b[1] - a[1])
.slice(0, 3)
.map((pair) => pair[0]);
console.log(sorted);

update/merge array values in React Redux store correctly without duplicates

My initial state is like below and if new Book added or price is changed then new updated array is coming from service whose result i need to merge in my initial state.
const initialState = {
booksData: [
{"Code":"BK01","price":"5"},
{"code":"BK02","price":"30"},
{"code":"BK03","price":"332"},
{"code":"BK04","price":"123"}
]
};
Updated array from server with few records updated/new
data: [
{"Code":"BK01","price":"10"},
{"code":"BK02","price":"25"},
{"code":"BK05","price":"100"}
]
updated state should become after merging updated array with old array.
booksData: [
{"Code":"BK01","price":"10"},
{"code":"BK02","price":"25"},
{"code":"BK03","price":"332"},
{"code":"BK04","price":"123"},
{"code":"BK05","price":"100"}
]
I would filter out elements of the old data that are in the new data, and concat.
const oldBooks = booksData.filter(book => !newData.some(newBook => newBook.code === book.code));
return oldBooks.concat(newData);
Keep in mind you must NOT push values into the old array. In your reducer you MUST create new instances, here a new array. 'concat' does that.
You can first merge both the array together and then reduce it to remove duplicates like
var booksData = [
{"code":"BK01","price":"5"},
{"code":"BK02","price":"30"},
{"code":"BK03","price":"332"},
{"code":"BK04","price":"123"}
]
var newData = [
{"code":"BK01","price":"10"},
{"code":"BK02","price":"25"},
{"code":"BK05","price":"100"}
]
const result = [...newData, ...booksData].reduce((res, data, index, arr) => {
if (res.findIndex(book => book.code === data.code ) < 0) {
res.push(data);
}
return res;
}, [])
console.log(result);
Merge the two array and filter using 'Code' property
const initialState = {
booksData: [
{ "Code": "BK01", "price": "5" },
{ "code": "BK02", "price": "30" },
{ "code": "BK03", "price": "332" },
{ "code": "BK04", "price": "123" }
]
};
const data =
[
{ "Code": "BK01", "price": "10" },
{ "code": "BK02", "price": "25" },
{ "code": "BK05", "price": "100" }
]
let newState = [...initialState.booksData, ...data];
newState = newState.filter((obj, pos, arr) => {
return arr.map(mapObj => mapObj['Code']).indexOf(obj['Code']) !== pos;
});
console.log(newState);
Collection of Objects
Filter a merged array to pick only non-existent items by iterating every item in the merged array which its index is before the current index of the "parent" filter iterator
const mergedUnique = [
...[{id:1}, {id:2}, {id:3}],
...[{id:1}, {id:4}, {id:2}]
]
.filter((item, idx, arr) =>
!arr.some(({id}, subIdx) => subIdx < idx && id == item.id)
)
console.log( mergedUnique )
Basic technique for "simple" arrays
Merge some arrays and filter them to pick only non-existent items by checking if the same item exists anywhere before the current item's index in the merged array.
lastIndexOf is used to check backwards, if the current value exists already, which contributes to keeping the order of the merged array in a certain way which might be desirable, which can only be achieved by checking backward and not forward.
Skip checking the first item - is obviously not a duplicate.
const mergedUniqe = [...[1,2,3], ...[1,3,4,5,2]] // [1, 2, 3, 1, 3, 4, 5, 2]
.filter((item, idx, arr) =>
!~arr.lastIndexOf(item, idx-1) || !idx
)
console.log( mergedUniqe )

Returning data after sorting through multiple arrays

I have nested arrays within an object, and I need to extract these values, and sort them into alphabetical order which will then be displayed in a table.
I am using the localeCompare method but it is returning:
Cannot read property 'localeCompare' of null
To be honest, I am not sure I am approaching this correctly. Using sort you can compare a to b to sort them into alphabetical order. I am getting confused how to compare the values from the arrays within a and b. I am using the first sort on tableData to access the array, and the using a second sort to compare the values that I pushed to array clientRefArr
if(params.sorting().clientRef) {
var clientRefArr = [];
tableData.sort(function(a, b){
a.renewalUIs.forEach(function(data, i){
clientRefArr.push(data.patentUI.clientRef);
})
return clientRefArr.sort(function(a, b){
console.log(a.localeCompare(b))
// return a.localeCompare(b)
})
})
orderedData = tableData;
}
return orderedData;
Question
Why is the error Cannot read property 'localeCompare' of null being returned?Am I approaching this issue completely wrong?
//JSON
[
0: {
transRef: "IX1000013"
renewalProgress: 30
renewalUIs: [
0: {
patentUI: {
business: null
clientRef: P0101011 // this is the required value
}
renewalAttemptsMade: 1
renewalDueDate: 1514764740000
}
]
},
1: {
transRef: "IX100333"
renewalProgress: 55
renewalUIs: [
0: {
patentUI: {
business: null
clientRef: P0101011 // this is the required value
}
renewalAttemptsMade: 1
renewalDueDate: 1514764740000
},
1: {
patentUI: {
business: null
clientRef: P5551011 // this is the required value
}
renewalAttemptsMade: 3
renewalDueDate: 174834740000
}
]
}
]
You could take a default value for both parts
(a || '').localeCompare(b || '')
for sorting the null values or even all falsy values to top.
An attempt with the given data (I changed some value, to get some sorting).
It now sorts the inner arrays renewalUIs first and then it takes the first element for sorting the outer array.
var array = [{ transRef: "IX1000013", renewalProgress: 30, renewalUIs: [{ patentUI: { business: null, clientRef: "P9101011" }, renewalAttemptsMade: 1, renewalDueDate: 1514764740000 }] }, { transRef: "IX100333", renewalProgress: 55, renewalUIs: [{ patentUI: { business: null, clientRef: "P7101011" }, renewalAttemptsMade: 1, renewalDueDate: 1514764740000 }, { patentUI: { business: null, clientRef: "P5551011" }, renewalAttemptsMade: 3, renewalDueDate: 174834740000 }] }];
array.forEach(function (o) {
o.renewalUIs.sort(function (a, b) {
return a.patentUI.clientRef.localeCompare(b.patentUI.clientRef);
});
});
array.sort(function (a, b) {
return a.renewalUIs[0].patentUI.clientRef.localeCompare(b.renewalUIs[0].patentUI.clientRef);
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
IF my interpretation is correct, it'd be way easier to first put everything in an single array and then sort it as such just use loops for the first task and a normal sort for the second, nested sorts are really just a bad idea.
// let's assume your data is in unorderedData
var clientUIs = [];
for (var i = 0; i < unorderedData.length; i++){
for (var j = 0 ; j < unorderedData[i]["renewalUIs"].length; j++){
// add each UI to your list individually.
clientUIs.push(unorderedData[i]["renewalUIs"][j]["patentUI"]);
}
}
clientUIs.sort() //just sort everything
tableData = clientUIs;
If "renewalUIs" or "patentUIs" arent constant you can iterate over the keys of the dictionary.

Array Sorting in an object in javascript

I have to sort the arrays of the "Key" in ascending order and here's what I am doing .
Surprsingly the first array is getting sorted in descending order and the next two in ascending order . Whats the possible reason for this?
var MainObject4 = [{ "mainarray": [{ "Key": [9,768,78] },
{ "Key": [9,4,1] },{ "Key": [49,89,54] }]
}];
var first = MainObject4[0];
Object.keys(MainObject4[0]).forEach(function (k) {
first[k].forEach(function (j) {
Object.keys(j).forEach(function (g) {
j[g].sort();
},this);
},this);
},this);
alert(JSON.stringify(MainObject4, 0, 4));
Expected output:
[9,78,768]
[1,4,9]
[49,54,89]
Output I am getting now:
[768,78,9]
[1,4,9]
[49,54,89]
See the doc of Array.sort()
The default sort order is according to string Unicode code points.
If you want to compare numbers, you need to provide the comparison function, as said in the doc :
To compare numbers instead of strings, the compare function can simply subtract b from a.
You need to add a function to compare sort parameters:
var MainObject4 = [{
"mainarray": [{
"Key": [9, 768, 78]
}, {
"Key": [9, 4, 1]
}, {
"Key": [49, 89, 54]
}]
}];
var first = MainObject4[0];
Object.keys(MainObject4[0]).forEach(function(k) {
first[k].forEach(function(j) {
Object.keys(j).forEach(function(g) {
j[g].sort(function(a, b) {
return a - b;
});
}, this);
}, this);
}, this);
alert(JSON.stringify(MainObject4, 0, 4));
var MainObject4 = [{ "mainarray": [
{ "Key": [9,768,78] },
{ "Key": [9,4,1] },
{ "Key": [49,89,54] }
]
}];
MainObject4[0].mainarray.forEach(function (j) {
Object.keys(j).forEach(function (g) {
j[g].sort(function (a, b) {
return a - b;
});
},this);
},this);
alert(JSON.stringify(MainObject4, 0, 4));
It's happening because of JavaScript's sort method takes by default unicode to compare the elements. For sorting numbers, you have to explicitly write a call back function to compare elements. For ex
var input = [1,20,2,12];
console.log(input.sort());// prints 1,12,2,20
console.log(input.sort(function(a,b){
return a-b;
}));// prints 1,2,12,20
So, You just need to add the compare function to your sort. That's all

Categories