I need to organize a dynamic javascript array that is retrieved from the database each time. The requirement is to sort it based on predefined values in an established order within a standard array.
For example, consider my dynamic array:
var dbArray = ['Apple','Banana','Mango','Apple','Mango','Mango','Apple'];
And let's say the reference array for sorting the above array looks like this:
var stdArray = ['Mango','Apple','Banana','Grapes'];
So, after sorting dbArray according to stdArray, the resulting array should be:
var resultArray = ['Mango','Mango','Mango','Apple','Apple','Apple','Banana'];
This custom sorting approach allows arranging the elements in any desired order specified within stdArray, regardless of traditional alphabetical or other common sorting methods.