私は自分の問題の解決策を見つけるために少し探し回っていましたが、それを完全に修正するものは何もありません。基本的に、この関数はソートを行いますが、テーブル内の数値を1から10までの数値だけでソートしません。
local numbers = {18, 45, 90, 77, 65, 18, 3, 57, 81, 10}
local function selectionSort(t)--t is the table to be sorted
local t = {18, 45, 90, 77, 65, 18, 3, 57, 81, 10}
local tkeys = {}
for k in pairs(t) do table.insert(tkeys, k) end
table.sort(tkeys)
for _, k in ipairs(tkeys) do print(k, t[k]) end
return t -- return the sorted table
end
list = selectionSort(list)
これが出てくるものです
1 18
2 45
3 90
4 77
5 65
6 18
7 3
8 57
9 81
10 10
そして私が欲しいのは
3 18
10 45
18 90
18 77
45 65
57 18
65 3
77 57
81 81
90 10
解決策はありますか?
key
入力からを取得していて、値が必要です。
次のように変更できます。
local list = {18, 45, 90, 77, 65, 18, 3, 57, 81, 10}
local function selectionSort(t)--t is the table to be sorted
local tSorted = {}
for _,v in pairs(t) do
table.insert(tSorted, v)
end
table.sort(tSorted)
for i=1,#t,1 do
print(tSorted[i], t[i])
end
return tSorted -- return the sorted table
end
list = selectionSort(numbers)
そして、あなたは得るでしょう:
sorted original
3 18
10 45
18 90
18 77
45 65
57 18
65 3
77 57
81 81
90 10
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加