How can I sort an array by string?
const filter = [
{ index: "First" },
{ index: "Second" },
{ index: "Third" }
]
const data = [
{ title: 'Apple', index: "Second" },
{ title: 'Samsung', index: "First" },
{ title: 'BMW', index: "Third" },
{ title: 'Apple', index: "Second" },
{ title: 'Apple', index: "Second" },
{ title: 'Samsung', index: "First" }
]
Expected results:
const data = [
{ title: 'Samsung', index: "First" },
{ title: 'Samsung', index: "First" },
{ title: 'Apple', index: "Second" },
{ title: 'Apple', index: "Second" },
{ title: 'BMW', index: "Third" }
]
How to iterate over two arrays correctly? Do I have to iterate over two arrays to do this? Or is there another way to get the desired result?
Given three inputs - "First", "Second", "Third", if there will be more of these indices?
4 Answers 4
- Using
Array#reduce
, iterate overfilter
and save each object'sindex
attribute and its index in the array in aMap
- Using
Array#sort
, sortdata
by the values of theindex
attributes from the above map
const
filter = [ { index: "First" }, { index: "Second" }, { index: "Third" } ],
data = [ { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" }, { title: 'BMW', index: "Third" }, { title: 'Apple', index: "Second" }, { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" } ];
const indexSortMap = filter.reduce((map, { index }, i) => map.set(index, i), new Map);
data.sort(({ index: a }, { index: b }) => indexSortMap.get(a) - indexSortMap.get(b));
console.log(data);
Use Array#sort
with custom compareFunction and Array#findIndex
with custom callback function
const filter = [ { index: "First",}, {index: "Second",}, {index: "Third",}];
const data = [ { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" }, { title: 'BMW', index: "Third" }, { title: 'Apple', index: "Second" }, { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" } ];
data.sort((a ,b) => {
return filter.findIndex(i => i.index === a.index) - filter.findIndex(i => i.index === b.index);
});
console.log(data);
One possible solution (and simple) is by iterating these two arrays. They will be pushed according to the order of the "filter" array:
const sortedArray = []
filter.forEach(fil=> {
data.forEach(dat=>{
if(dat.index===fil.index)
sortedArray.push(dat)
})
})
console.log(sortedArray)
One approach would be to push the objects to three different arrays, each one for an index and then concatenate the arrays.
var arr1 = [],arr2 = [],arr3 = [], data = [ { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" }, { title: 'BMW', index: "Third" }, { title: 'Apple', index: "Second" }, { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" } ];
for(d of data) d.index == 'First' ? arr1.push(d) : d.index == 'Second' ? arr2.push(d) : arr3.push(d);
data = [].concat.apply([], [arr1, arr2, arr3]);
console.log(data);
filter
was an array of strings instead of an array of objects. Then something likedata.sort((a, b) => filter.indexOf(a.index) - filter.indexOf(b.index))
would be possible. ButfindIndex
could be used here as well.