# Rule Prioritization (RuleSet Ordering)

Source: https://appfig.com/docs/rule-prioritization

Lets you control which rule "wins" when multiple rules could apply to the same user, by setting an explicit evaluation order for the RuleSets inside a feature.

## How Evaluation Works

- Each feature can have multiple **RuleSets** — rule blocks, each with their own conditions and return value (a static `value` or an [A/B test](https://appfig.com/docs/ab-testing))
- RuleSets are evaluated **top to bottom**, in the order shown in the dashboard UI
- The **first RuleSet whose conditions match** the user wins — its value (or A/B test assignment) is returned and evaluation stops
- If no RuleSet matches, no value is returned for that feature (`getFeatureValue()` / `getConfig()` returns `null`/`nil`)

This is the same "first match wins" behavior already described in [Rule Schema](https://appfig.com/docs/rule-schema) and [Core Concepts](https://appfig.com/docs/concepts) — this page documents how you control the order.

**Example:**

```json
Feature: "veteran_bonus"
  RuleSet 1 (Priority #1): IF user completed 50+ levels → value = "3x"
  RuleSet 2 (Priority #2): IF user completed 10+ levels → value = "2x"
  (No match) → feature returns null
```

A user with 60 completed levels matches both RuleSets, but RuleSet 1 is evaluated first and wins — they get `"3x"`, not `"2x"`.

## Dashboard Controls

- **Priority badge** - each RuleSet displays a numbered badge (`Priority #1`, `Priority #2`, ...) showing its position in the evaluation order
- **Up / Down buttons** - reorder a RuleSet by moving it up or down relative to its siblings
  - The "up" button is disabled on the first RuleSet
  - The "down" button is disabled on the last RuleSet
- **Info banner** - a short reminder above the RuleSet list: "RuleSets are evaluated top to bottom. The first matching rule's value is applied."

Reordering takes effect the next time rules are published — no separate "activate ordering" step.

## Under the Hood

- **No new database fields.** Priority is simply the array position of the RuleSet as stored — there's no explicit `priority` integer field to keep in sync.
- **No backend/serializer changes.** The existing pipeline already preserves array order end-to-end (Firestore → JSON → CDN).
- **No SDK changes.** SDKs already iterate RuleSets sequentially and return on the first match (see [Rule Schema](https://appfig.com/docs/rule-schema)), so reordering in the dashboard is immediately effective once published — no SDK update is required on any platform.

## Scope

This ordering applies **within a feature** — it controls which of that feature's own RuleSets wins. It does **not** control the order in which different features are evaluated relative to each other; that's left entirely to your app's own logic (e.g. which `getFeatureValue()` calls you make and in what order).

## Next Steps

- **[Rule Schema Reference](https://appfig.com/docs/rule-schema)** - Full condition and return-value reference
- **[A/B Testing](https://appfig.com/docs/ab-testing)** - Use A/B test variants as a RuleSet's return value
- **[Core Concepts](https://appfig.com/docs/concepts)** - "First Match Wins" principle
