I have seen questions and answers how to sort array by one value (text or number) and by two numbers (years and count of something).
How can I sort by one string in Ascending order and another string in special order?
Here is one object from array
var stop = {
type: "S", // values can be S, C or H. Should ordered S, C and then H.
street: "SW Dummy St." // Should be sorted in ascending order
}
And expected end result should look like this
var data = [
{ type: 'S', year: 'SW Karp' },
{ type: 'S', year: 'SW Walker' },
{ type: 'C', year: 'SW Greth' },
{ type: 'C', year: 'SW Main' }
{ type: 'H', year: 'SW Dummy' }
];
type
, returning e.g. a number for each letter, such as0
forS
,1
forC
and2
forH
:data.sort(sort_by({name: 'type', primer: function(x) { return ({'S':0, 'C':1, 'H':2})[x];}}, 'street'));
– Felix Kling Mar 8 at 13:40