How would I convert a table from {4,1,3,2} to {4,3,2,1}?

I attempted using `table.sort()`

but it ordered it from smallest to largest.

How would I convert a table from {4,1,3,2} to {4,3,2,1}?

I attempted using `table.sort()`

but it ordered it from smallest to largest.

4 Likes

Because that is the sorting method by default.

```
local t = { 4, 1, 3, 2 }
table.sort(t, function(a, b)
return a > b
end)
```

13 Likes

How does that work?

I don’t really understand it, sorry.

Alright this is how it works.

```
sort(t: { [number]: any }, predicate?: (any, any) -> bool)
```

The `table.sort`

iterates over all elements and calls `predicate(a, b)`

on them. `predicate(a, b)`

evaluates to true when `a`

should precede `b`

. by default the sorting method is `a < b`

, hence why your table was sorted in ascending order

2 Likes