2016-07-19 8 views
1

Es scheint, dass lodashs sortedIndex ein vorwärts sortiertes Array erwartet, damit seine binäre Suche funktioniert. (z. B. [0,1,2,4])SortedIndex für umgekehrt sortiertes Array?

Gibt es eine Möglichkeit, sortierteIndexBy zu verwenden, wenn das Array umgekehrt sortiert wird? (z.B. [4,2,1,0])?

> _.sortedIndex([0,1,2,4], 3) 
> 3 
> _.sortedIndex([4,2,1,0], 3) 
> 4 

Um dies jetzt zu arbeiten, ich habe die Anordnung umgekehrt, die sortedIndex finden, das neue Element einzufügen, und dann die Array un-umkehren.


Hinweis - brauchen etwas, das für die Sortierung von Strings sowie Zahlen funktioniert.

['A','B','D'] in ['D','B','A'] und einfügen 'C'.

Antwort

2

Wie wäre es mit _.sortedIndexBy?

Bearbeitet: Für string Vergleich kann String.prototype.charCodeAt() Ihnen helfen, es in Number umzuwandeln, dann kann die gleiche Logik angewendet werden.

const arr1 = [0, 1, 2, 4]; 
 
const arr2 = [4, 2 ,1, 0]; 
 

 
console.log(_.sortedIndex(arr1, 3)); 
 
// Similar, but with ranking function. 
 
console.log(_.sortedIndexBy(arr2, 3, function(x) {return -x;})); 
 

 
const charArr = ['D','B','A']; 
 
// Take the first char and convert to Number 
 
let index = _.sortedIndexBy(charArr, 'C', function(x) { 
 
    // Type checks. (If you want it to be general to many types.. 
 
    if (typeof x === 'string') { 
 
    return -x.charCodeAt(0); 
 
    } else if (typeof x === 'number') { 
 
    return -x; 
 
    } // else ... for other types..... 
 
}); 
 

 
console.log('To insert char C, put it to index: ', index);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>

oder durch _.sortedIndex, hat es auch iteratee Rang vor 4.0.0

const arr1 = [0, 1, 2, 4]; 
 
    const arr2 = [4, 2 ,1, 0]; 
 

 
    console.log(_.sortedIndex(arr1, 3)); 
 
    console.log("Reversed order without ranking func: ",_.sortedIndex(arr2, 3)); 
 
    // Ranking function to inverse the order. 
 
    console.log("Reversed order with ranking func: ",_.sortedIndex(arr2, 3, function(x) {return -x;}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js"></script>

Wenn Sie wissen, ob das Array umgekehrt ist oder nicht , können Sie einfach die index von Original 01 bekommen, dann manuell den realIndex berechnet, der bei arr.length - index wäre.

const arr = ['AAA', 'AAB', 'AAD']; 
 

 
function mySoretdIndex(arr, target, isReversed) { 
 
    let sortedIndex = _.sortedIndex(arr, target); 
 
    return isReversed ? arr.length - sortedIndex : sortedIndex; 
 
} 
 

 
console.log('Find \'AAC\' should be inserted at', mySoretdIndex(arr, 'AAC', true));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>

+0

Gute Idee! Leider muss ich manchmal Strings auf diese Weise sortieren. [A, B, D] in [D, B, A] und füge C ein. – jedierikb

+0

Für 'string' Fall aktualisiert. – fuyushimoya

+0

Aber für längere Strings ('AAA', 'AAB', 'AAC') müsste man die Zeichenkette durchlaufen, um jedes Zeichen zu konvertieren. Ich fürchte, es könnte schneller sein, an diesem Punkt umzukehren. – jedierikb