How to sort numbers without repeating (table.sort)

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?

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] lastValAdded = t[i] end end

–john

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

This code has worked with me :smiley: :smiley: :smiley:

 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

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

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} -- ADDED: 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, ", " ) )

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

Glad to help, personally I really like Moses. :slight_smile:

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

Thanks Davebollinger
Your smart way.
I loved her.

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}"

:rolleyes: :rolleyes: :rolleyes: :rolleyes:

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

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] lastValAdded = t[i] end end

–john

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

This code has worked with me :smiley: :smiley: :smiley:

 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

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

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} -- ADDED: 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, ", " ) )

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

Glad to help, personally I really like Moses. :slight_smile:

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