# last

Source: https://taxilang.org/docs/language/stdlib/last

**Full name:** `taxi.stdlib.last`

## Signature

```taxi
declare extension function <T> last(collection: T[]):T
```

## Description

Returns the last item within the collection. Throws an error if the collection is empty.

This function retrieves the final element in an ordered collection.

```taxi
// Get the oldest transaction from a date-sorted list
find { account: Account } as {
   oldestTransaction: account.transactions.last()
}
```

## Examples

This example demonstrates how to use `last()` to retrieve the oldest transaction for an account.

Schema:

```taxi
model Transaction {
   date: String
   amount: Decimal
   description: String
}

model Account {
   accountNumber: String
   transactions: Transaction[] // Assumed to be in descending date order (newest first)
}
```

Query:

```taxi
given {
   account: Account = {
      accountNumber: 'ACC-12345',
      transactions: [
         { date: '2023-05-01', amount: 150.00, description: 'Grocery Store' },
         { date: '2023-04-28', amount: 45.99, description: 'Online Shop' },
         { date: '2023-04-25', amount: 20.00, description: 'Transfer' }
      ]
   }
}
find {
   accountNumber: account.accountNumber
   // Using .last() against the type
   oldestTransaction: Transaction =  Transaction[].last()
   // or, alternatively, using against the property name
   alsoOldestTransaction: Transaction = account.transactions.last()
}
```

Result:

```json
{
  "accountNumber": "ACC-12345",
  "oldestTransaction": {
    "date": "2023-04-25",
    "amount": 20,
    "description": "Transfer"
  },
  "alsoOldestTransaction": {
    "date": "2023-04-25",
    "amount": 20,
    "description": "Transfer"
  }
}
```
