2

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?

Majed Badawi
28.5k4 gold badges30 silver badges56 bronze badges
asked Oct 21, 2021 at 13:50
1
  • 1
    It’d be easier if filter was an array of strings instead of an array of objects. Then something like data.sort((a, b) => filter.indexOf(a.index) - filter.indexOf(b.index)) would be possible. But findIndex could be used here as well. Commented Oct 21, 2021 at 13:52

4 Answers 4

1
  • Using Array#reduce, iterate over filter and save each object's index attribute and its index in the array in a Map
  • Using Array#sort, sort data by the values of the index 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);

answered Oct 21, 2021 at 13:54
1

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);

answered Oct 21, 2021 at 14:04
0

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)
answered Oct 21, 2021 at 13:56
0

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);

answered Oct 21, 2021 at 14:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.