# Filter Rows and Sort a CSV Naturally (Room 2 before Room 10)

**Use case:** 

Filter a pasted CSV with AND/OR rules, remove rows by regex, sort with natural order so Room 2 comes before Room 10, number the rows and compute years of service. Replace the CSV with your own file URL or dataset.

## Input

```json
{
  "items": [
    {
      "id": 1,
      "name": "  Trail Backpack ",
      "price": "89.90",
      "stock": 12,
      "tags": [
        "outdoor",
        "bag"
      ],
      "seller": {
        "name": "Nordic Gear",
        "country": "NO"
      },
      "created": "2025-03-14T09:30:00Z"
    },
    {
      "id": 2,
      "name": "Camp Stove",
      "price": "$45.00",
      "stock": 0,
      "tags": [
        "outdoor",
        "cooking"
      ],
      "seller": {
        "name": "Fire & Co",
        "country": "US"
      },
      "created": "2025-01-02T10:00:00Z"
    },
    {
      "id": 3,
      "name": "Wool Socks 10-pack",
      "price": "24",
      "stock": 150,
      "tags": [],
      "seller": {
        "name": "Nordic Gear",
        "country": "NO"
      },
      "created": "2025-02-20T08:15:00Z"
    },
    {
      "id": 4,
      "name": "Headlamp 300",
      "price": "39.5",
      "stock": 8,
      "tags": [
        "light"
      ],
      "seller": {
        "name": "Fire & Co",
        "country": "US"
      },
      "created": "2024-11-30T17:45:00Z"
    },
    {
      "id": 5,
      "name": " Water Bottle 1L",
      "price": "12",
      "stock": 40,
      "tags": [
        "bottle",
        "steel"
      ],
      "seller": {
        "name": "Nordic Gear",
        "country": "NO"
      },
      "created": "2025-03-01T12:00:00Z"
    },
    {
      "id": 6,
      "name": "Rain Jacket",
      "price": "129",
      "stock": 5,
      "tags": [
        "clothing"
      ],
      "seller": {
        "name": "Alpine Ltd",
        "country": "CH"
      },
      "created": "2025-02-05T07:20:00Z"
    }
  ],
  "inlineSources": [
    {
      "name": "staff",
      "format": "csv",
      "text": "employee,room,department,salary,start\nDana Cole,Room 10,Sales,52000,2023-04-01\nEli Park,Room 2,Support,41000,2022-09-15\nFay Ross,Room 1,Sales,61000,2021-01-10\nGus Hill,Room 11,Support,38000,2024-02-01\nHana Ito,Room 3,Sales,47000,2020-07-20\nIvo Zed,Room 9,Legal,90000,2019-05-05\n"
    }
  ],
  "operations": [
    {
      "op": "filter",
      "where": {
        "any": [
          {
            "field": "department",
            "op": "equals",
            "value": "Sales"
          },
          {
            "all": [
              {
                "field": "salary",
                "op": "gt",
                "value": 40000
              },
              {
                "field": "department",
                "op": "in",
                "value": [
                  "Support",
                  "Legal"
                ]
              }
            ]
          }
        ]
      }
    },
    {
      "op": "filter",
      "action": "remove",
      "where": {
        "field": "employee",
        "op": "regex",
        "value": "^Gus"
      }
    },
    {
      "op": "sort",
      "by": [
        {
          "field": "room",
          "order": "asc"
        }
      ],
      "natural": true
    },
    {
      "op": "add_column",
      "column": "row",
      "rowNumber": true
    },
    {
      "op": "add_column",
      "column": "years",
      "expr": "round(days_between(start, '2025-01-01') / 365.25, 1)"
    },
    {
      "op": "select",
      "fields": [
        "row",
        "employee",
        "room",
        "department",
        "salary",
        "years"
      ]
    }
  ],
  "onError": "null",
  "exports": [
    "csv"
  ],
  "exportName": "transformed",
  "escapeFormulas": true,
  "maxItems": 100000,
  "maxOutputRows": 1000000,
  "maxFileSizeMb": 50,
  "csvInferTypes": true
}
```

## Output

```json
{
  "id": {
    "label": "ID"
  },
  "name": {
    "label": "Name"
  },
  "price": {
    "label": "Price",
    "format": "number"
  },
  "price_with_tax": {
    "label": "Price incl. tax",
    "format": "number"
  },
  "seller": {
    "label": "Seller"
  },
  "country": {
    "label": "Country"
  },
  "tags": {
    "label": "Tags"
  },
  "year": {
    "label": "Year"
  }
}
```

## How it works

### 1. Sign up on Apify or log in

The Dataset Transform: Rename, Flatten, Filter & Sort Columns Actor is ready to run with the inputs already set for "Filter Rows and Sort a CSV Naturally (Room 2 before Room 10)". [Sign up or log in](https://console.apify.com/create-task-from-example/v01wKwKWa64UK9bFJ) and you'll land in Apify Console with a copy of this exact task.

### 2. Start the "Filter Rows and Sort a CSV Naturally (Room 2 before Room 10)" task

The Dataset Transform: Rename, Flatten, Filter & Sort Columns Actor will start running based on the input automatically. You can also change all the input fields, like pasted items (json rows), pasted csv or json text, and more, to match your specific use case.

### 3. Receive the "Filter Rows and Sort a CSV Naturally (Room 2 before Room 10)" output

Monitor the progress in real time. You will be notified as soon as your dataset is complete and ready for review. Each result includes data such as ID, name, price, and more.

### 4. Integrate into your workflow

The final output is delivered in JSON, CSV, or Excel format, ready to be plugged into your workflow. You can also [run the "Filter Rows and Sort a CSV Naturally (Room 2 before Room 10)" task via API](https://apify.com/data-done-right/dataset-transform.md).

## About this Actor

This example demonstrates how to use [Dataset Transform: Rename, Flatten, Filter & Sort Columns](https://apify.com/data-done-right/dataset-transform.md) with a specific input configuration. Visit the [Actor detail page](https://apify.com/data-done-right/dataset-transform.md) to learn more, explore other use cases, and run it yourself.


## How to integrate an Actor?

This Task's input is already configured above. Use it as-is rather than inventing a new one.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For full API examples (JavaScript, Python, CLI, MCP, OpenAPI), see this Task's Actor page: https://apify.com/data-done-right/dataset-transform.md

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).
