farzher / fuzzysort

Fast SublimeText-like fuzzy search for JavaScript.

Home Page:https://rawgit.com/farzher/fuzzysort/master/test/test.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sorting data that is stored as SoA (Structures of Arrays) as opposed to AoS (Arrays of Structures)

sumzary opened this issue · comments

If I have an array ids[1000] and an array names[1000],
currently, I have to create an intermediate array of objects[{ id, name },{}...]
in order to be able to sort by names, and have access to the id as well.

currently, I have to create an intermediate array of objects[{ id, name },{}...]

yup. what's the problem?
here's an optimized way to do it by attaching your id to an internal fuzzysort object to avoid creating new garbage objects
(this requires names to be unique though)
(should be faster but haven't tested)

ids = new Array(1000)
names = new Array(1000)
names[69] = 'steve'

var results = fuzzysort_soa('steve', names)
var name = names[results[0].index]
var id   = ids[results[0].index]
console.log(name, id)

function fuzzysort_soa(search, names, options) {
  var prepared_array = []
  for(var i=0; i<names.length; i++) {
    prepared_array[i] = fuzzysort.prepare(names[i])
    prepared_array[i].index = i
  }
  return fuzzysort.go('steve', prepared_array, options)
}