Rank

A Dart package that helps to implement a string index.

Overview

Before:

class Element {
  const Element(..., this.position);

  ...
  final int position;  // 4
}

With a order number based ranking system, re-ordering a element of a list may require updating all elements of the list, which is O(n).

elementposition
......
apple3
from _ to hereOh ho!
No room between 3 and 4.
pear4
raspberry5

Well, let's try adding zeros.

elementposition
......
apple300
from _ to hereNow, you can add 99 elements
before changing anything.
pear400
raspberry500

What if we don't use numbers?


With rank

class Element {
  const Element(..., this.rank);
  ...
  
  final String rank; // knar
}

Using a string-based range, this is done with O(1). All you have to do is to update the rank field of the reordered element.

elementrank
......
applekra
a sweet fruit addedkran
pearkrb
raspberrykrc

Then, to get your sorted list you could perform this query in your database:

SELECT * FROM "_" ORDER BY "rank";

Usage

First, add the plugin rank to your pubspec.yaml file:

dependencies:
  rank: ^0.1.1

Import rank in files that it will be used:

import 'package:rank/rank.dart';

Next, you must initialize the plugin:

Rank rank = Rank();

To get a rank use generate

sweetFruitRank = rank.generate(previous: appleRank, next: pearRank);

Issues

Please file any issues, bugs or feature request as an issue on our GitHub page.

Contributing

If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature or you'd like an easier or better way to do something), consider opening a pull request.

Libraries

rank