2016-03-25 14 views
3

Javascript verfügt über eine eingebaute Sortiermethode für Arrays.Javascript Sortierfunktion, die die Schlüssel zurückgibt

Was ich brauche, sind die Schlüssel des Arrays, nicht die Werte. Ich möchte nicht, dass das Array geändert wird. Gibt es einen offensichtlichen Weg, diese Schlüssel zu bekommen (bevor ich meine eigene Art zu schreiben ...)?

Beispiel:

var fruit = ["pear", "apple", "lemon"]; 
fruit.sort(); 
// now fruit is ["apple", "lemon", "pear"]; 

Aber was will ich wissen, ist

keys = [1,2,0] ; 

So kann ich es wie folgt verwendet werden:

var fruit = ["pear", "apple", "lemon"]; 
var keys = keysOfSortedArray(fruit); 
for(var i in keys) { 
    console.log(fruit[key[i]]); 
} 

Vielen Dank für so schnell helfen . Alles von Dir.

Antwort

1

diesen Code Versuchen:

var fruit = ["pear", "apple", "lemon"]; 
var sorted = [].concat(fruit).sort(); 

var sortedIndex = sorted.map(function(item) { 
    return fruit.indexOf(item) 
}); 
console.log(sortedIndex) // prints [1, 2, 0]; 
+0

'var sortedIndex = sort.map (item => fruit.indexOf (item)); ' – John

+0

Danke, das löst es –

+0

@EmmanuelDelay Gut zu helfen –

1

Sie müssen eine Sicherungskopie nehmen (nicht tief, seicht Backup tun wird) vor dem Sortieren

var fruit = ["pear", "apple", "lemon"]; 
var backup = fruit.slice(); 
fruit.sort(); 
var output = fruit.map(function(val){ return backup.indexOf(val) }); 

Ihre Funktion keysOfSortedArray wird als

function keysOfSortedArray(arr) 
{ 
    var backup = arr.slice(); 
    backup.sort(); 
    return backup.map(function(val){ return arr.indexOf(val) }); 
} 
+0

Danke, das funktioniert auch –

1

Hier können Sie schauen: Sorting with map

// the array to be sorted 
 
var fruit = ["pear", "apple", "lemon"]; 
 

 
// temporary array holds objects with position and sort-value 
 
var mapped = fruit.map(function (el, i) { 
 
    return { index: i, value: el.toLowerCase() }; 
 
}) 
 

 
// sorting the mapped array containing the reduced values 
 
mapped.sort(function (a, b) { 
 
    return +(a.value > b.value) || +(a.value === b.value) - 1; 
 
}); 
 

 
// container for the resulting order 
 
var result = mapped.map(function (el) { 
 
    return fruit[el.index]; 
 
}); 
 

 
// get the wanted keys 
 
var keys = mapped.map(function (el) { 
 
    return el.index; 
 
}); 
 

 
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>'); 
 
document.write('<pre>' + JSON.stringify(keys, 0, 4) + '</pre>');

+0

Danke, ich werde mich darum kümmern –

0

Dies funktioniert:

ES5

function sortIndexes (a) { 
    var aClone = a.slice(0); 
    return a.sort().map(function(x){ 
     return aClone.indexOf(x); 
    }); 
} 

var fruit = ["pear", "apple", "lemon"]; 
console.log(sortIndexes(fruit)); 

Mit Prototype und ES5:

Array.prototype.sortIndexes = function() { 
    var aClone = this.slice(0); 
    return this.sort().map(function (x) { 
     return aClone.indexOf(x); 
    }); 
}; 

var fruit = ["pear", "apple", "lemon"]; 
console.log(fruit.sortIndexes()); 

ES6(viel Schönheit)

let sortIndexes = (a) => { 
    var aClone = a.slice(0); 
    return a.sort().map(x => aClone.indexOf(x)); 
} 


let fruit = ["pear", "apple", "lemon"]; 
console.log(sortIndexes(fruit)); 

Mit Prototype und ES6 (viel Schönheit und besser):

Array.prototype.sortIndexes = function(){ 
    let aClone = this.slice(0); 
    return this.sort().map(x => aClone.indexOf(x)); 
} 

let fruit = ["pear", "apple", "lemon"]; 
console.log(fruit.sortIndexes()); 
Verwandte Themen