I have the following array:
var my_list = ["XXXS", "XXS", "XS", "S", "M", "L", "XL", "XXL", "XXXL"];
Then I've the following array of objects that I get from json:
my_json = [
{
size: "L",
color: "black"
}, {
size: "M",
color: "blue"
}, {
size: "XXL",
color: "red"
}, {
size: "XXS",
color: "red"
}
];
I'm trying to sort the array of objects by the size
basing in the order of my_list
I've found sort functions, but it sorts alphabetically, I don't know if there is a simple way to do it.
Here is a jsfiddle where you can try: http://jsfiddle.net/ew3ZU/3/
To be clear:
ACTUAL OUTPUT
[{"size":"L","color":"black"},{"size":"M","color":"blue"},{"size":"XXL","color":"red"},{"size":"XXS","color":"red"}]
DESIRED OUTPUT
[{"size":"XXS","color":"red"},{"size":"M","color":"blue"}{"size":"L","color":"black"},{"size":"XXL","color":"red"}]