Table.sort not working

Hi there I’m trying to sort this list, but table.sort keeps returning nil

This is my table. I’m trying to get a table ordered alphabetically by hero.

    local heroes = 
    {
    	{
    		["hero"] = "Zack",
    		["hp"] = 70,
    		["attack"] = 10,
    	},
    	{
    		["hero"] = "Mark",
    		["hp"] = 60,
    		["attack"] = 30,
    	},{
    		["hero"] = "Beto",
    		["hp"] = 99,
    		["attack"] = 60,
    	},
    }
 local function sort_abc(a,b)
   	return a.hero < b.hero
 end
 heroes = table.sort(heroes, sort_abc)
 pprint(heroes)

Nevertheless pprint, prints nil.

I tried with something simple such as:

local t = {5,7,3}
t = table.sort(t)
pprint(t)

With the same result.

Could anyone explain me what’s going on?

Thanks a lot.

It says “bit” in the function, is that right?

1 Like

From table.sort documentation:

“Sorts table elements in a given order, in-place, from table[1] to table[n]”

This means that the table.sort function doesn’t return a new table, instead it sorts the table you pass to it.

1 Like

Thanks for answering Alex, it was b instead of bit. I made a type while formatting the code for the post.

same result I got nil. Do you know why?

thanks

1 Like

I see that makes sense. I worked now thanks Britzl :smile:

1 Like