order-by
order the records of one file (or array) by the order of keys in another
npm install -g order-by
Usage
% cat xrs4477212 AArs3094315 AArs3131972 GGrs12124819 AArs11240777 GG# we'll sort these according to their order in the second file% cat yrs11240777rs12124819rs3094315rs3131972rs4477212# boom% order-by x 1 yrs11240777 GGrs12124819 AArs3094315 AArs3131972 GGrs4477212 AA
The method also works on objects, but must be called from javascript:
// we'd like to sort this by the order in bvar a = key:'one'value:'x'key:'two'value:'x'key:'two'value:'y'key:'three'value:'x'key:'wee'value:'y'key:'four'value:'x'// our ordervar b = 'four''two''three''one'var orderBy =// use the value of 'key' in a when sorting according to b/*[ { key: 'four', value: 'x' },{ key: 'two', value: 'x' },{ key: 'two', value: 'y' },{ key: 'three', value: 'x' },{ key: 'one', value: 'x' },{ key: 'wee', value: 'y' } ]*/
Note that elements which aren't in the order which is supplied are sorted at the end (under key undefined
).
Elements with the same key are sorted at the same position.
Rationale
This module allows a sorted order in one file to be applied to any other.
License
MIT