1

I have 2 arrays. Names in first array and indexes in second array. I ordered with C# (Array.sort() property).

Example : my array name is OrderedNames.

var names = OrderedName.SelectMany(x => x.Names)      // names array
var indexes = OrderedName.SelectMany(x => x.Indexes) // indexes array
Array.sort(indexes,names)                           // ordered names with indexes

I did this C# with LINQ.

I want to do the same thing with JavaScript.

So my Javascript code as the following(for example):

var names = Enumerable.From('$.OrderedNames').SelectMany('$.Names').ToArray();
var indexes = Enumerable.From('$.OrderedNames').SelectMany('$.Indexes').ToArray();

I can choose names and indexes. I want to merge two arrays and sorted. I did this with C# Array.sort() but I didn't with Javascript.

Do you have any idea please?

3
  • 1
    I can tell you now your JS code isn't going to compile. You can't use .NET functionality (LINQ) directly from JS like that. Commented Aug 3, 2016 at 19:43
  • how can I do the same process with JavaScript I ask ? I know, I can't use .Net functionality directly from JS. @ext0 Commented Aug 3, 2016 at 19:48
  • Check stackoverflow.com/documentation/javascript/187/arrays/4206/… Commented Aug 3, 2016 at 19:49

2 Answers 2

1

You can combine the names and keys together, sort them, and then return the sorted names like this

var names = ['two', 'three', 'one', 'four'];
var keys = [2, 3, 1, 4];

var sortedNames = names
                    .map((n, i) => { return { name: n, key: keys[i] }})
                    .sort((a, b) => a.key - b.key)
                    .map(n => n.name);

console.log(sortedNames); // ['one', 'two', 'three', 'four']';
Sign up to request clarification or add additional context in comments.

Comments

0

Interesting question. I think without map.sort.map you can do it like this too..

var names = ['rhino', 'hippo', 'tiger', 'badger', 'crocodile'],
     keys = [4, 5, 3, 1, 2],
    clone = names.slice(),
   sorted = names.sort((a,b) => keys[clone.indexOf(a)]-keys[clone.indexOf(b)]);
console.log(sorted);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.