# `rank`: sort records by how likely the answer is yes.

*The outage comes first and the quote due today second. The bill due in a month comes third, and the newsletter that needs no reply comes last.*

```
question="Is this urgent?"

cat <<'EOF' |
Newsletter: our autumn catalog is here. No reply needed.
Our checkout page is down and customers cannot pay
Reminder: your invoice is due in 30 days
Please send the signed quote by 5 pm today
EOF
thinkthen rank "$question" --batch 1
```

*Output*

```
Our checkout page is down and customers cannot pay
Please send the signed quote by 5 pm today
Reminder: your invoice is due in 30 days
Newsletter: our autumn catalog is here. No reply needed.
```

*exit 0: the run finished*

You give it one yes-or-no question and many records. You get back every record again, most likely first.

## Read the answer

rank takes no threshold. It drops nothing, and nothing is not sure. The sort happens on this machine.

| Exit code | What it means |
| --- | --- |
| 0 | the run finished |
| 2 | usage or input error |
| 4 | the backend failed or refused, as it does for evidence over the size limit |
| 5 | a local failure |
| 70 | a defect in the tool |

## Call it from your language

The Bash example opens this page.

```
import thinkthen as tt

question = "Is this urgent?"
inbox = [
    (
        "Newsletter: our autumn catalog is here. "
        "No reply needed."
    ),
    "Our checkout page is down and customers cannot pay",
    "Reminder: your invoice is due in 30 days",
    "Please send the signed quote by 5 pm today",
]
by_urgency = tt.rank(question, inbox).value
order = [one["index"] for one in by_urgency]
assert order == [1, 3, 2, 0]
```

No Polars sample for `rank`.

No pandas sample for `rank`.

```
import assert from "node:assert/strict";
import * as tt from "thinkthen";

const question = "Is this urgent?";
const inbox = [
  "Newsletter: our autumn catalog is here. " +
    "No reply needed.",
  "Our checkout page is down and customers cannot pay",
  "Reminder: your invoice is due in 30 days",
  "Please send the signed quote by 5 pm today",
];
const byUrgency = (await tt.rank(question, inbox)).value;
const order = byUrgency.map((one) => one.index);
assert.deepEqual(order, [1, 3, 2, 0]);
```

```
require "thinkthen"

question = "Is this urgent?"
inbox = [
  "Newsletter: our autumn catalog is here. " \
    "No reply needed.",
  "Our checkout page is down and customers cannot pay",
  "Reminder: your invoice is due in 30 days",
  "Please send the signed quote by 5 pm today"
]
by_urgency = ThinkThen.rank(question, inbox).value
raise unless by_urgency.map(&:index) == [1, 3, 2, 0]
```

No R sample for `rank`.

```
use thinkthen::{Engine, Question};

let tt = Engine::from_env()?;
let question = "Is this urgent?";
let urgent = Question::rank(question)?;
let inbox = [
    concat!(
        "Newsletter: our autumn catalog is here. ",
        "No reply needed.",
    ),
    "Our checkout page is down and customers cannot pay",
    "Reminder: your invoice is due in 30 days",
    "Please send the signed quote by 5 pm today",
];
let by_urgency = tt.rank(&urgent, inbox)?.into_value();
let order: Vec<&str> = by_urgency
    .iter()
    .map(|one| *one.input())
    .collect();
assert_eq!(order, [inbox[1], inbox[3], inbox[2], inbox[0]]);
```

Put this code inside `fn main() -> Result<(), Box<dyn std::error::Error>>` and end it with `Ok(())`. `main` returns a `Result`, so `?` compiles.

```
#include <assert.h>
#include <json-c/json.h>
#include <thinkthen.h>

thinkthen_engine *tt = thinkthen_engine_new();
assert(tt);

const char *rank =
    "{\"rank\": \"Is this urgent?\", "
    "\"records\": ["
    "\"Newsletter: our autumn catalog is here. "
    "No reply needed.\", "
    "\"Our checkout page is down and customers "
    "cannot pay\", "
    "\"Reminder: your invoice is due in 30 days\", "
    "\"Please send the signed quote by 5 pm today\"]}";
const int expected[] = {1, 3, 2, 0};
char *rank_call = thinkthen_call(tt, rank);
assert(rank_call);
enum json_tokener_error parse_error;
struct json_object *result =
    json_tokener_parse_verbose(rank_call, &parse_error);
assert(parse_error == json_tokener_success);
assert(result && json_object_get_type(result)
    == json_type_object);
struct json_object *value;
json_bool has_value = json_object_object_get_ex(
    result, "value", &value);
assert(has_value);
struct json_object *facts;
json_bool has_facts = json_object_object_get_ex(
    result, "facts", &facts);
assert(has_facts);
assert(facts && json_object_get_type(facts)
    == json_type_object);
assert(json_object_array_length(value) == 4);
for (size_t i = 0; i < 4; i++) {
    struct json_object *index;
    struct json_object *ranked =
        json_object_array_get_idx(value, i);
    json_bool has_index =
        json_object_object_get_ex(ranked, "index", &index);
    assert(has_index);
    assert(json_object_get_int(index) == expected[i]);
}
json_object_put(result);
thinkthen_free_string(rank_call);
thinkthen_engine_free(tt);
```

Keep the `#include` lines on top. Put the rest inside `int main(void)` and end it with `return 0;`.

No C++ sample for `rank`.

No Objective-C sample for `rank`.

No COBOL sample for `rank`.

No Ada sample for `rank`.

No Java sample for `rank`.

No Kotlin sample for `rank`.

No Scala sample for `rank`.

No C# sample for `rank`.

No Go sample for `rank`.

No Swift sample for `rank`.

No Zig sample for `rank`.

No PHP sample for `rank`.

No Dart sample for `rank`.

```
LOAD './thinkthen.duckdb_extension';

SELECT body
FROM (
    SELECT
        body,
        thinkthen_probability('Is this urgent?', body)
            AS urgency
    FROM (VALUES
        ('Newsletter: our autumn catalog is here. ' ||
         'No reply needed.'),
        ('Our checkout page is down and customers ' ||
         'cannot pay'),
        ('Reminder: your invoice is due in 30 days'),
        ('Please send the signed quote by 5 pm today')
    ) t(body)
)
ORDER BY urgency DESC;
```

*What DuckDB printed*

```
body
Our checkout page is down and customers cannot pay
Please send the signed quote by 5 pm today
Reminder: your invoice is due in 30 days
Newsletter: our autumn catalog is here. No reply needed.
```

No SQLite sample for `rank`.

```
WITH t(id, body) AS (VALUES
    ('1', 'Newsletter: our autumn catalog is here. ' ||
          'No reply needed.'),
    ('2', 'Our checkout page is down and ' ||
          'customers cannot pay'),
    ('3', 'Reminder: your invoice is due in 30 days'),
    ('4', 'Please send the signed quote by 5 pm today')
)
SELECT body
FROM (
    SELECT t.body, d.probability AS urgency
    FROM t
    JOIN thinkthen_decide_many(
        '{"decide": "Is this urgent?"}',
        (SELECT jsonb_object_agg(id, body) FROM t)
    ) AS d ON d.key = t.id
) AS ranked
ORDER BY urgency DESC;
```

*What PostgreSQL printed*

```
                           body                           
----------------------------------------------------------
 Our checkout page is down and customers cannot pay
 Please send the signed quote by 5 pm today
 Reminder: your invoice is due in 30 days
 Newsletter: our autumn catalog is here. No reply needed.
(4 rows)
```

[Arguments, options, and more examples](/reference/functions/rank/)

## Jobs that use `rank`

- [Rank the inbound leads](/how-tos/rank-the-inbound-leads/): for sales teams

Watch `rank` answer questions about Beatles songs: [rank sorts by your criteria.](/learn/beatles-bench/rank/)

On GitHub: [github.com/botassembly/thinkthen](https://github.com/botassembly/thinkthen)
