I have just made this, maybe it’s not the best way but it works:
local ui = require ("ui")
--Create default scores
local scores = {}
for i = 1, 10, 1 do
scores[i] = math.random(1,10)
end
local place = {}
for i = 1, 10, 1 do
place[i] = ui.newLabel {
text = i..".",
textColor = {255, 255, 255, 255},
size = 50,
align = "left",
bounds = {0, 10, display.contentWidth/5, 40},
};
place[i].x = display.contentWidth/8
if i == 1 then
place[i].y = display.contentHeight/7
else
place[i].y = place[i-1].y + 80
end
end
--Create all lists in an array
local list = {}
--Place lists and set their scores
for i = 1, 10, 1 do
list[i] = display.newText(scores[i], 0, 0, native.systemFontBold, 50)
list[i].x = display.contentWidth/2
if i == 1 then
list[i].y = display.contentHeight/7+40
else
list[i].y = list[i-1].y + 80
end
end
--Sort the scores
local function sort()
for i = 1, 5, 1 do
for i = 10, 1, -1 do
print(scores[i-1])
if i\>1 then
if scores[i] \> scores[i-1] then
local tempScore = scores[i]
scores[i] = scores[i-1]
scores[i-1] = tempScore
end
end
end
--Update the list
print (scores[i])
for i = 1, 10, 1 do
list[i].text= scores[i]
end
end
end
local btn = display.newImage("image.png")
btn:addEventListener("touch", sort)
[import]uid: 24111 topic_id: 7943 reply_id: 28474[/import]