Перейти к содержанию

Дайджесты за январь-февраль

Обновления гайдов и аддонов

Январь Февраль

Мониторинг серверов и редактор аддонов

Представляем вам две легенды. То, о чем можно было только мечтать, стало реальностью.

Мониторинг серверов Редактор аддонов

Подсказки из игры на вашем сайте

Теперь вы можете отображать сведения о внутриигровых элементах простым наведением курсора мыши.

Подробнее

Апдейтер аддонов

Представляем вам программу для автообновления аддонов и делимся подробностями.

Подробнее Скачать

How to sort a string-indexed-table after the strings in the indexes?


Гость Carnifex

Рекомендуемые сообщения

So, I have a table similar to this:

t={["playername1"]=1,["playername2"]=0,["playername3"]=0,["playername4"]=0}

and i want to sort this table in lexicographical order after the strings=player-names in the indexes of the table.

How can I do this, because with "for i,m in t..." I get a seemingly random order of the playernames?

BTW: I need this for my new addon TS3Viewer.

Ссылка на комментарий
Поделиться на другие сайты

Are you sure it is REALLY string-indexed? (NOT WString-indexed, because WStrings are userdata) ?

If there are WString indexes, then it is epic fail.

Ссылка на комментарий
Поделиться на другие сайты

no, they are string indexed (ts does not know wstrings), but they are sorted in the for loop not after the alphabet, but in the row I write them into the table

so for example:

t["d"]=1

t["a"]=2

t["z"]=0

for n,i in t do

LogInfo(n)

end

returns

d

a

z

and not

a

d

z

Ссылка на комментарий
Поделиться на другие сайты

Code:
t = {}
t ["d"] =1
t ["a"] =2
t ["z"] =0

local function Sort( tab, dir )
local t = {}
for k in pairs( tab ) do table.insert( t, k ) end
if dir then table.sort( t, function( a, b ) return a > b end ) else table.sort( t ) end
--table.sort( t, function( a, b ) return dir and a > b or a < b end )
for _, v in ipairs( t ) do LogInfo( tab [ v ]  ) end
end

Sort( t )


upd: Commented line was replaced by a common if-else statement, because, in this case, using and-or construction with existing "dir" breaks the sort function.
Ссылка на комментарий
Поделиться на другие сайты

Yes, I've also had a similar idea of a alternative sorting algorithm.

But there is no solution for sorting it in-place, right?

Ссылка на комментарий
Поделиться на другие сайты

Sorting by VALUES it extremely easy, like:

Code:
table.sort( t, function(a,B) return a.name < b.name end )

but sorting by KEYS is more tricky:

http://lua-users.org/wiki/SortedIteration

Anyway, you will need some extra function(s) to do that. But Duvodas provided a much simplier solution.

P.S. Don't look at GuildGui1b TERRIBLE sorting function. It is the WORST example, because it sorts by VALUES without use of table.sort(). It just needs a total rewrite, but I was too lazy to touch it yet... There are good sorting examples in DarkDPSMeter.

Ссылка на комментарий
Поделиться на другие сайты

BTW: Is there with our access to lua models a way to check, if the file exists before running dofile(file)?

Ссылка на комментарий
Поделиться на другие сайты

Гость
Ответить в этой теме...

×   Вставлено с форматированием.   Восстановить форматирование

  Разрешено использовать не более 75 эмодзи.

×   Ваша ссылка была автоматически встроена.   Отображать как обычную ссылку

×   Ваш предыдущий контент был восстановлен.   Очистить редактор

×   Вы не можете вставлять изображения напрямую. Загружайте или вставляйте изображения по ссылке.

×
×
  • Создать...

Важная информация

Пользуясь сайтом, вы принимаете Условия использования