Sorting Javascript array of objects by specific string order?
问题 I have the following data: and I was wondering how I could sort these in a specific order. The order needs to be: Yellow, Blue, White, Green, Red and then within those colors, it would show the smallest number first. So in this case, the correct sorting should be: y2, y3, b0, b2, b6, w2, g7, r4 Does anyone have any ideas on how to accomplish that? I'm using underscore.js if that makes it easier. 回答1: This naively assumes that all elements have a valid color (or at least it sorts elements with