0

Given some randomly arranged array of day strings:

var days = ['Tues', 'Thur', 'Mon', 'Wed']

with days[0] = 'Tues', days[1] = 'Thurs', etc. After sorting, we get

sortedDays = ['Mon', 'Tues', 'Wed', 'Thur']

However, I'd like to reference the indices of the newly-sorted array to the old array, i.e., the old array has indices (0, 1, 2, 3) while the new array has indices (2, 0, 3, 1).

I have no idea on how to achieve this.

2
  • 1
    Could you elaborate on what you mean by you'd like to reference the indices? Do you simply want to know the order of items in the second array with regard to the original? Commented Nov 27, 2018 at 5:47
  • Yeap, I'd just like to know how the indices changed after the sort. Commented Nov 27, 2018 at 6:47

3 Answers 3

2

Array.prototype.indexOf() would be what you need here to find the position (index) of an element within an array. Simply call .indexOf() on every element in your sortedDays array.

const days = ['Tues', 'Thur', 'Mon', 'Wed'];
const sortedDays = ['Mon', 'Tues', 'Wed', 'Thur'];
const sortedDaysIndices = sortedDays.map(day => days.indexOf(day));
console.log(sortedDaysIndices);

Sign up to request clarification or add additional context in comments.

3 Comments

This is a little problematic if there are duplicate entries since it will give the same index for both. It's not clear in the OP's question is that's an issue of not.
Fair point! But if there are duplicates how do you know which one of the duplicate in the sorted array would reference which index in the original array?
Right, it may not matter at all since the dupes are not distinct in any way.
0

If you want to get the sorted indexes you can start with an array from [0...n] and then sort that array based on the sort order of the days.

For example:

// Determines how we will we sort the days:
const sortDays = {
    Mon:  0,
    Tues: 1, 
    Wed:  2,
    Thur: 3,
    Fri:  4,
    Sat:  5,
    Sun:  6
}

const days = ['Tues', 'Thur', 'Mon', 'Wed', 'Thur', 'Sun', 'Mon'];
// plain indices
let index = Array.from(days, (_,i) => i)
index.sort((a, b) => sortDays[days[a]] - sortDays[days[b]])

// now indices are sorted according to days sort order
console.log("indices: ", index.join(','))

// you can use the index to sort days:
sortedDays = index.map(i => days[i])
console.log("sorted days:", sortedDays.join(','))

This has the advantage of giving you the correct indices even when you have duplicates in the original list.

Comments

0

var days = ['Tues', 'Thur', 'Mon', 'Wed'];

var sortedDays = ['Mon', 'Tues', 'Wed', 'Thur'];

var indexMapping = {}
days.forEach((item, index)=> (indexMapping[item] = { 
    oldIndex: index  , newIndex: sortedDays.indexOf(item)}
 )
)

console.log(indexMapping)

Comments

Your Answer

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.