No announcement yet.

Control Sorting

  • Filter
  • Time
  • Show
Clear All
new posts

  • Control Sorting

    I have a table like so:

     Items = {}
     Items[0]={COL1="Apples",COL2=1.77,COL3="1.1 MB"}
     Items[1]={COL1="Oranges",COL2=1.8,COL3="56.8 GB"} 
     Items[2]={COL1="Bananas",COL2=3,COL3="19 B"} 
     Items[3]={COL1="Pears",COL2=7,COL3="44 MB"}
     Items[4]={COL1="Grapes",COL2=2.5,COL3="77 KB"}
    The table index is the ID of the item as well as the initial index.
    I want to sort the table ascending or descending for alphabetic and numeric.
    When the user selects the item the variable 'ItemSel' is set to the current index.
    The control is redrawn every half a millisecond or on interaction (sorting/selecting) and it is darw by what is in the 'Items' table.
    I need some help with the following functions:

     function SortAlphaAss()
     function SortAlphaDec()
     function SortNumAss()
     function SortNumDec()
     function MapIDtoIndex()
     function MapIndexToID()
    I'm open to changing the method if its easier to code but the end result needs to be a functioning table sort that maps the ID to the index so that on redraw the control sorts the relevant column.

    There is a £20 (UK sterling) PayPay payment to anyone that can do this.

  • #2
    Hi Shrek

    When we look at the your table structure , you have used 0 as the starting index of that table
    even if there is no problem with that , you know that Lua tables (numerically indexed) are 1 based
    in order to implement a proper table iteration (if it will be sorted then) you should use 1 based index

    if your custom control is 0 based then you can easily convert index/ID by using a +1 or -1 on the value

    simply when you want to access this table by the item index of your custom control ,use +1 on that index to access table

    for example you selected first item in your control , and its index is 0
    when you want to access associated data on your lua table


    this is a shortcut to your MapIDtoIndex() and MapIndexToID() functions

    i would explain more if i could have some more info about your control

    i created a detailed example ,that sorts COL1 as string ,COL2 as number
    and COL3 as file size ,it also compares file size against KB ,MB ,GB etc..

    you only need to align index to/from table , something like below

    for i = 0 ,#Items do
    and when you add an item to your control which will return a zero based index
    just use table.insert() that will automatically assign an index to associated data

    in addition to these , sort algorithm does not change index , only its value
    this will also avoid that index confusion

    here is the sample project :
    Attached Files Is Closed.

    Facebook Page


    • #3
      Cheers mate, sent you the donation but you have the note field turned off so couldn't link to this.

      I shall port your code to a sample and check it out, should have made the sample first really :yes


      • #4
        Originally posted by Shrek View Post
        Cheers mate, sent you the donation but you have the note field turned off so couldn't link to this.

        I shall port your code to a sample and check it out, should have made the sample first really :yes
        Hi ,sorry for late reply and thanks for donation :yes

        I created that donation link in 2006 or 2007 and did not know what i am doing at the moment :o

        I can make modifications on the sort function according your project requirements upon your request

        Thanks Is Closed.

        Facebook Page


        • #5
          Cheers, just finished moving home and haven't ported it over yet but I will be back with an example of what I'm trying to achieve


          • #6
            thanks reteset :yes


            • #7
              @reteset, this is pretty good, do you think you could add % to the options? say at most 2 decimal places but on whole %'s no decimal.


              • #8

                I could not understand what did you mean by percentage
                If you could elaborate a bit more then i would help on that as well

                Please show an example of data that you want to be sorted
       Is Closed.

                Facebook Page


                • #9
                  Hi, thanks, well never mind about the % because I forgot I had the raw data to make the % so I just sorted that instead.