2017-09-08 5 views
2

나는 다음과 같은 루아 테이블 변수 T에 저장된 :Lua의 내부 테이블을 "score"와 "index"로 정렬하는 방법은 무엇입니까?

{ 
    ["mn"] = { ["index"] = 7, ["key"] = "mn", ["score"] = 0 }, 
    ["kl"] = { ["index"] = 6, ["key"] = "kl", ["score"] = .4 }, 
    ["ef"] = { ["index"] = 3, ["key"] = "ef", ["score"] = .3 }, 
    ["ab"] = { ["index"] = 1, ["key"] = "ab", ["score"] = 0 }, 
    ["cd"] = { ["index"] = 2, ["key"] = "cd", ["score"] = .1 }, 
    ["gh"] = { ["index"] = 4, ["key"] = "gh", ["score"] = 0 }, 
    ["ij"] = { ["index"] = 5, ["key"] = "ij", ["score"] = .2 } 
} 

나는 다음과 같은 방법으로 T 테이블의 내부 테이블의 모든 정렬하려면 : score가 넣어 높은와
1. 테이블 상단.
2. 등호가 score 인 표는 index으로 분류됩니다.

{ 
    [1] = { ["index"] = 6, ["key"] = "kl", ["score"] = .4 }, -- highest "score" 
    [2] = { ["index"] = 3, ["key"] = "ef", ["score"] = .3 }, 
    [3] = { ["index"] = 5, ["key"] = "ij", ["score"] = .2 }, 
    [4] = { ["index"] = 2, ["key"] = "cd", ["score"] = .1 }, 
    [5] = { ["index"] = 1, ["key"] = "ab", ["score"] = 0 }, -- lowest "score", lowest "index" 
    [6] = { ["index"] = 4, ["key"] = "gh", ["score"] = 0 }, -- when scores are the same, sort by their "index" instead 
    [7] = { ["index"] = 7, ["key"] = "mn", ["score"] = 0 } -- lowest "score", highest "index" 
} 

방법이 루아 테이블 정렬을 달성하기 :

그래서, 정렬 한 후, 다음과 같은 순서 표는 출력을 생산해야 하는가?

+0

내가 사용해야하는 것으로 알고 https://devdocs.io/lua~5.3/index#pdf-table.sort – hjpotter92

+0

'table.sort' 루아 기능 하지만이 경우에는 어떻게 사용할지 모르겠다. – Pojat

답변

1

먼저 해시를 테이블로 변환 한 다음 score (내림차순)으로 정렬 한 다음 index (오름차순)으로 정렬하는 사용자 지정 정렬 함수를 사용하여 해당 테이블의 요소를 정렬해야합니다. 같은 점수.

이런 식으로 뭔가 작업을해야합니다 :

local hash = { 
    ["mn"] = { ["index"] = 7, ["key"] = "mn", ["score"] = 0 }, 
    ["kl"] = { ["index"] = 6, ["key"] = "kl", ["score"] = .4 }, 
    ["ef"] = { ["index"] = 3, ["key"] = "ef", ["score"] = .3 }, 
    ["ab"] = { ["index"] = 1, ["key"] = "ab", ["score"] = 0 }, 
    ["cd"] = { ["index"] = 2, ["key"] = "cd", ["score"] = .1 }, 
    ["gh"] = { ["index"] = 4, ["key"] = "gh", ["score"] = 0 }, 
    ["ij"] = { ["index"] = 5, ["key"] = "ij", ["score"] = .2 } 
} 
local tbl = {} 
for _,v in pairs(hash) do 
    table.insert(tbl, v) 
end 
table.sort(tbl, function(a,b) 
    return a.score > b.score or a.score == b.score and a.index < b.index 
    end) 
+0

도움 주셔서 감사합니다! :디 – Pojat

1

루아에서는 표가 두 개의 데이터 구조 인 배열과 사전을 포함합니다. 각 요소는 숫자 인덱스와 인덱스와 연관되어 배열을 정렬

정렬 수단은 연속되었는지는 1,2,3 ...

하면 초기 테이블 실제로 사전이다 - 각각의 엔트리는 임의의 보유 그것과 관련된 키 (귀하의 경우 문자열입니다).

따라서 설명하는 것은 실제로 정렬 작업이 아니며 결국 다른 종류의 표를 원하게됩니다.

table.sort은 lua 테이블의 배열 부분, 즉 인덱스가 1에서 시작하고 처음 엔 nil 항목에서 끝나는 요소에 적용됩니다.

a={s=3,['r']=3, 5,3,2, nil,21} 
       |these| 
       |ones | 

그래서, 먼저 배열을 생성 한 그런 종류 : 또는

local sorted={} 
for k,v in pairs(T) do 
    table.insert(sorted,v) 
end 
table.sort(sorted,function(a,b) 
    --your code here 
    --function should return true if element `a` from the array `sorted` 
    --must be higher (to the left of) `b` 
end) 

, 당신은 모두 사전 및 배열 부분에서 같은 테이블의 항목을 저장할 수는 table.sort 기능은 무시 것이다 사전. 그러나 pairs을 사용하여 테이블을 반복하고 동시에 새 요소를 추가하는 것은 바람직하지 않습니다. 따라서 관용적 인 방법은 여전히 ​​중간 복사본을 포함합니다.

+0

설명해 주셔서 감사합니다. – Pojat