rankShopCategories function

List<ShoppingCategory> rankShopCategories (
  1. List<ShoppingCategory> categories,
  2. ShoppingContext context
)

Implementation

List<ShoppingCategory> rankShopCategories(
    List<ShoppingCategory> categories, ShoppingContext context) {
  List<ShoppingCategory> sortedCategories = categories;
  String query = context.query;
  Levenshtein editDistance = Levenshtein();
  sortedCategories.sort((ShoppingCategory lhs, ShoppingCategory rhs) {
    double lhsDist = editDistance.normalizedDistance(lhs.displayName, query);
    double rhsDist = editDistance.normalizedDistance(rhs.displayName, query);
    return lhsDist.compareTo(rhsDist);
  });
  return sortedCategories;
}