-3

array or arrays

var items = [
    ['alice','bob', 'emma', 'isabella'],
    ['emma','sofia', 'alice', 'bob'],
    ['bob','emma', 'alice']
];

how to map items in concrete order ['alice','emma','bob', ..other]

every array can have items like alice, bob and emma and other

Alex
  • 8,908
  • 28
  • 103
  • 157
  • 2
    What do you mean by "*map items in concrete order*"? Are you asking to `.sort()` them? – Bergi Aug 31 '20 at 21:19
  • items.map((item) => ( //here item i want to be in order alice, bob, emma, others – Alex Aug 31 '20 at 21:21
  • 2
    And how is that order chosen? Is it an alphabetical order, a random made-up one, the one of the first array in `items`? – Bergi Aug 31 '20 at 21:22
  • you want sorted the item array for the first occurence in the inner-array or you want the name-arrays are sorted? – Sascha Aug 31 '20 at 21:23
  • Add commas (`,`) after all-but-last inner arrays. Then, use this: `items.map(item=>item.sort())`. – iAmOren Aug 31 '20 at 21:26
  • Javascript is not like Haskell or Prolog, It will not try to match literals. – Dominique Fortin Aug 31 '20 at 21:29
  • @ iAmOren sort will order alphabetically, but how to order by given list – Alex Aug 31 '20 at 21:29
  • so if the input is `[1, 2, 3, 4, 5]`, then defines `concrete` order is `[3, 2, 4, 5]`, then the output will be `[3, 2, 4, 5, 1]`? – Sphinx Aug 31 '20 at 21:36
  • @Bergi in case we speak only about an array, for e.g `['emma','sofia', 'alice', 'bob']` how to reorder it in `['alice','emma','bob','sofia']`, other e.g. `['bob','sofia', 'emma', 'alice']` reorder in `['alice','emma','bob','sofia']` – Alex Aug 31 '20 at 21:38
  • @Sphinx yes, like your example, but only with strings instead of numbers – Alex Aug 31 '20 at 21:40
  • Use sort() and subtract reference index a from reference index b. Assumes you have a preordained order for all items included in your arrays – charlietfl Aug 31 '20 at 21:42
  • 1
    @Alex So [this](https://stackoverflow.com/q/13304543/1048572) is what you were looking for? – Bergi Aug 31 '20 at 21:46
  • @Bergi yup, thx – Alex Aug 31 '20 at 21:48
  • the link @Bergi provided should give you some idea. but if the order of `other` elements is not matter, probably you will want to try this one: `function sort(src, concrete) { let orderDict = concrete.reduce((pre, cur, index) => { pre[cur] = index + 1 return pre }, {}) return src.map(items => { return items.sort((a, b) => { let t1 = orderDict[a] || 9999999 let t2 = orderDict[b] || 9999999 return t1 - t2 }) }) }` – Sphinx Aug 31 '20 at 21:52

1 Answers1

1

var items = [
    ['alice','bob', 'emma', 'isabella'],
    ['emma','sofia', 'alice', 'bob'],
    ['bob','emma', 'alice']
];

const NAME =  ['alice','bob', 'emma'];
let res = [];

items.forEach(names => {
    names.sort((a,b) => {
        indA = NAME.indexOf(a);
        indB = NAME.indexOf(b);
        if (indA!=-1)
            return (indB!=-1) ? indA-indB : -1;
        else return (indB!=-1) ? 1 : 0;
    });
    res.push(names);
});

console.log(res);
Sascha
  • 4,576
  • 3
  • 13
  • 34
  • You should return `0` if both `indA` and `indB` are `-1`, otherwise this is not a consistent comparison function. Easiest done by `return (indA==-1) - (indB==-1) || indA - indB;` – Bergi Aug 31 '20 at 21:49
  • 1
    @Bergi - You are right. Thanks, I corrected it. – Sascha Aug 31 '20 at 22:17