# Sorting, Sub-Sorting, and Comparing Table Key-Values

**URL:** <https://forums.solar2d.com/t/sorting-sub-sorting-and-comparing-table-key-values/357702>\
**Category:** Lua Language\
**Created:** [February 9, 2026, 4:11am UTC](https://forums.solar2d.com/t/sorting-sub-sorting-and-comparing-table-key-values/357702 "2026-02-09T04:11:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gilby](https://forums.solar2d.com/user_avatar/forums.solar2d.com/gilby/32/4911_2.png) [@Gilby](https://forums.solar2d.com/u/Gilby)\
**Post date:** [February 9, 2026, 4:11am UTC](https://forums.solar2d.com/t/sorting-sub-sorting-and-comparing-table-key-values/357702/1 "2026-02-09T04:11:07Z")

</div>

Good day, to those reading this.

I do hope that what follows holds the attention of a helpful-patient-someone(s)  
that may assist me in what must truly be trivial matters to most.  
Thank you in advance for reading this.

Using the table.sort() function, I am successfully ordering a topScores{table} by a certain game time, in seconds.

topScores[1].sec = 38 SECONDS

Now, in the case where two or more table entries (top scores) may have the same game time,  
I would like to further sort those specific entires by the “game points”.

topScores[1].pts = 54 POINTS

Of course, still under the ‘sort-restraints’ of the first table.sort() of the ‘sec’ KEY

ex.  
topScores = { UNSORTED  
{ pts = 54, sec = 38 }, THIS SHOULD BE IN 2ND PLACE, BASED ON ‘sec’ AND ‘pts’  
{ pts = 101, sec = 46 } THIS SHOULD BE IN 3RD PLACE, BASED ON ‘sec’  
{ pts = 99, sec = 38 }, THIS SHOULD BE IN 1ST PLACE, BASED ON ‘sec’ AND ‘pts’  
}

```
local holdSortedTable = {}
table.sort(topScores, function(a,b) return a.sec < b.sec end)
for i=1,5,1 do
    holdSortedTable[i] = topScores[i]
end
topScores = holdSortedTable

```

topScores = { SOME WHAT SORTED  
{ pts = 54, sec = 38 }, THESE NEED TO SWAP ie. 54 pts is less than 99 pts  
{ pts = 99, sec = 38 }, THESE NEED TO SWAP ie. 99 pts is greater than 54 pts  
{ pts = 101,sec = 46 }  
}

NOW I NEED TO SORT BY: ‘PTS’ POINTS  
NOTE: (only those entries with the same “sec” value, 38 in this case)

Question 1:  
Continuing with the code above, how would that additional sub-sorting of ‘pts’ be coded?

Question 2:  
And lastly, when it comes time to adding a new score, how do I iterate through the topScores{} table,  
such that, it looks for duplicate “sec” KEY VALUES?;  
so that a new score (possibly having the same game time as an existing score)  
is properly INSERTED, or ignored, based on the ‘points’ value?

newScore = { pts = 55, sec = 38 }

topScores = {  
{ pts = 99, sec = 38 },  
{ pts = 54, sec = 38 }, newScore INSERTED HERE { pts = 55, sec = 38 }  
{ pts = 101, sec = 46 } “BUMPED OFF” AND 2nd place becomes 3rd place { pts = 54, sec = 38 }  
}

Continuing with the code above, how would that additional “COMPARING” of ‘sec’ and ‘sec’ and of ‘pts’ and ‘pts’ with a new table entry be coded?

Thank you, again.

I appreciate your time, and any help you may have.

Chris

---

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [February 9, 2026, 4:33am UTC](https://forums.solar2d.com/t/sorting-sub-sorting-and-comparing-table-key-values/357702/2 "2026-02-09T04:33:00Z")

</div>

Hi.

Untested, but for your first case, you’d basically just do it in one go:

```auto
local function BetterScore (a, b)
  if a.sec == b.sec then
   return a.pts > b.pts -- more points is better, right?
  else
    return a.sec < b.sec
  end
end

table.sort(topScores, BetterScore)

```

For the second you should even be able to reuse that:

```auto
local index = 1
for i = 1, #topScores do
  if BetterScore(newScore, topScores[i]) then -- better?
    break
  else
    index = index + 1 -- will be #topScores + 1 at end, so an append
  end
end

table.insert(topScores, index, newScore)

```

---

<div class="post-metadata">

**Author:** ![Gilby](https://forums.solar2d.com/user_avatar/forums.solar2d.com/gilby/32/4911_2.png) [@Gilby](https://forums.solar2d.com/u/Gilby)\
**Post date:** [February 9, 2026, 3:55pm UTC](https://forums.solar2d.com/t/sorting-sub-sorting-and-comparing-table-key-values/357702/3 "2026-02-09T15:55:19Z")

</div>

Well, that was easy enough.  
Worked like a charm.

Thanks star crunch!

Much appreciated.
