[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

## How to sort numbers without repeating (table.sort) Started by w967 Apr 13 2019 01:26 AM

11 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

### w967

[GLOBAL: userInfoPane.html]
w967
• Observer

• 8 posts
• Corona SDK

We have this example

```local t = { 3,2,5,1,4 , 6 , 5 , 8, 2}

local function compare( a, b )
return a < b  -- Note "<" as the operator
end

table.sort( t, compare )
print( table.concat( t, ", " ) )  --> 1, 2, 2, 3, 4 , 5 , 5 , 6 , 8

```

Repeat (2) and (5)
How to sort numbers without repeating?

[TOPIC: post.html]
#2

### schizoid2k

[GLOBAL: userInfoPane.html]
schizoid2k
• Contributor

• 564 posts
• Corona SDK

There's a few ways to do this, but off the top of my head, after your code to sort the array, you can increment through the sorted list and add unique values to a new array.

I didn't test this code, but something like this.

```local newT = {}
local lastValAdded = t[1] - 1     -- seed so you have a value that is not in the array

for i = 1, #t do
if t[i] ~= lastVal then
newT[#newT + 1] = t[i]
end
end
```

--john

[TOPIC: post.html]
#3

### w967

[GLOBAL: userInfoPane.html]
w967
• Observer

• 8 posts
• Corona SDK

Thanks schizoid2k
I've opened the way.
I hope I succeed.

[TOPIC: post.html]
#4

### w967

[GLOBAL: userInfoPane.html]
w967
• Observer

• 8 posts
• Corona SDK

This code has worked with me

```        local n = 1
for i = 1, #t do
if t[n] == t[n + 1] then
table.remove(t, n)
n = n - 1
end
n = n + 1
end
```

This example is complete:

```local t = { 3,2,5,1,4 , 6 , 5 , 8, 2}

local function compare( a, b )
return a < b  -- Note "<" as the operator
end

table.sort( t, compare )
local n = 1
for i = 1, #t do
if t[n] == t[n + 1] then
table.remove(t, n)
n = n - 1
end
n = n + 1
end

print( table.concat( t, ", " ) )  --> 1, 2, 3, 4, 5, 6, 8
```

[TOPIC: post.html]
#5

### Yoger Games

[GLOBAL: userInfoPane.html]
Yoger Games
• Contributor

• 109 posts
• Corona SDK

I can also recommend the open source library moses, that has methods to solve this.

https://github.com/Yonaba/Moses/blob/master/doc/tutorial.md

[TOPIC: post.html]
#6

### davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
• Corona Geek

• 1,373 posts
• Corona SDK

another approach might be to invert your table, then invert it back, eliminating duplicates prior to sorting.  for example:

```-- GIVEN:
local t = { 3,2,5,1,4 , 6 , 5 , 8, 2}

local u = {}
for k,v in pairs(t) do u[v]=k end
t = {}
for k,v in pairs(u) do t[#t+1]=k end

-- GIVEN:
local function compare( a, b )
return a < b  -- Note "<" as the operator
end
table.sort( t, compare )
print( table.concat( t, ", " ) )
```

[TOPIC: post.html]
#7

### sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
• Contributor

• 593 posts
• Corona SDK

@Yoger, I like Yonoba's work but I'd never seen Moses before.  Thanks for the link!

[TOPIC: post.html]
#8

### Yoger Games

[GLOBAL: userInfoPane.html]
Yoger Games
• Contributor

• 109 posts
• Corona SDK

Glad to help, personally I really like Moses.

[TOPIC: post.html]
#9

### w967

[GLOBAL: userInfoPane.html]
w967
• Observer

• 8 posts
• Corona SDK

Thank you very much Yoger Games
The Moses Library is more than wonderful.

[TOPIC: post.html]
#10

### w967

[GLOBAL: userInfoPane.html]
w967
• Observer

• 8 posts
• Corona SDK

Thanks Davebollinger
I loved her.

[TOPIC: post.html]
#11

### w967

[GLOBAL: userInfoPane.html]
w967
• Observer

• 8 posts
• Corona SDK

From the Moses Library

-----------------------------------

unique (array)

Aliases: uniq.

Makes an array duplicate-free.

```M.unique {1,1,2,2,3,3,4,4,4,5} -- => "{1,2,3,4,5}"
```

[TOPIC: post.html]
#12

### sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
• Contributor

• 593 posts
• Corona SDK

Some shared Penlight on the Slack group a while ago and it's also a cool library.

[topic_controls]

[/topic_controls]