Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 532809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:26:37+00:00 2026-05-13T09:26:37+00:00

I have a key => value table I’d like to sort in Lua. The

  • 0

I have a key => value table I’d like to sort in Lua. The keys are all integers, but aren’t consecutive (and have meaning). Lua’s only sort function appears to be table.sort, which treats tables as simple arrays, discarding the original keys and their association with particular items. Instead, I’d essentially like to be able to use PHP’s asort() function.

What I have:

items = {
    [1004] = "foo",
    [1234] = "bar",
    [3188] = "baz",
    [7007] = "quux",
}

What I want after the sort operation:

items = {
    [1234] = "bar",
    [3188] = "baz",
    [1004] = "foo",
    [7007] = "quux",
}

Any ideas?

Edit: Based on answers, I’m going to assume that it’s simply an odd quirk of the particular embedded Lua interpreter I’m working with, but in all of my tests, pairs() always returns table items in the order in which they were added to the table. (i.e. the two above declarations would iterate differently).

Unfortunately, because that isn’t normal behavior, it looks like I can’t get what I need; Lua doesn’t have the necessary tools built-in (of course) and the embedded environment is too limited for me to work around it.

Still, thanks for your help, all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:26:37+00:00Added an answer on May 13, 2026 at 9:26 am

    You seem to misunderstand something. What you have here is a associative array. Associative arrays have no explicit order on them, e.g. it’s only the internal representation (usually sorted) that orders them.

    In short — in Lua, both of the arrays you posted are the same.

    What you would want instead, is such a representation:

    items = {
        {1004, "foo"},
        {1234, "bar"},
        {3188, "baz"},
        {7007, "quux"},
    }
    

    While you can’t get them by index now (they are indexed 1, 2, 3, 4, but you can create another index array), you can sort them using table.sort.

    A sorting function would be then:

    function compare(a,b)
      return a[1] < b[1]
    end
    
    table.sort(items, compare)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 279k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The files themselves are within the repository. If you want… May 13, 2026 at 3:20 pm
  • Editorial Team
    Editorial Team added an answer There are ways to do introspection in SQL and pull… May 13, 2026 at 3:20 pm
  • Editorial Team
    Editorial Team added an answer It has been deprecated. You'll probably have to use javascript,… May 13, 2026 at 3:20 pm

Related Questions

I have a table where I have added a new column, and I want
In MySQL, is it generally faster/more efficient/scalable to return 100 rows with 3 columns,
I've been researching to find out how and what to use to get say
If I have a number of users and a number of pages, and each
The thing is I have to create a 2D matrix in php where each

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.