Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sortedIndex for reverse sorted array?

It seems lodash's sortedIndex expects a forward sorted array for its binary search to work. (e.g. [0,1,2,4])

Is there a way to used sortedIndexBy when the array is reverse sorted? (e.g. [4,2,1,0])?

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

To get this to work now, I have to reverse the array, find the sortedIndex, insert the new element, and then un-reverse the array.


Note -- need something which works for sorting strings as well as numbers.

['A','B','D'] into ['D','B','A'] and insert 'C'.

like image 418
jedierikb Avatar asked May 26 '26 03:05

jedierikb


1 Answers

How about _.sortedIndexBy?

Edited: For string comparison, String.prototype.charCodeAt() can help you convert it to Number, then the same logic can be applied.

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>

or by _.sortedIndex, it also has iteratee to rank before 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>

Thanks to pilau: The sortedIndex expects the array is forward sorted, so we can't just put the reverse sorted array and get arr.length - index, and in order to handle vary scenario, I think we need to do either

  • Reverse array -> get sorted index and put -> reverse it again. or
  • Get reversed copy by slice and reverse -> get sorted index and calculate by arr.length - index -> inserted to origin array.

To achieve the expected result.

like image 176
fuyushimoya Avatar answered May 28 '26 16:05

fuyushimoya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!