Jump to content

sort a float list


Xed Saunders
 Share

You are about to reply to a thread that has been inactive for 1799 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

hello, i'm in the need to sort a list composed by float numbers

llListSort does not produce reliable results, and i am not able to find any example on the wiki.

the list is composed by dot separated values like this 1.345 2.453 65.432 23.433

i've tried to convert float to string before to insert them in the list but llListSort does not work either

any solution? thanks

Link to comment
Share on other sites

In what cases does this not produce a correct result?

default
{
    state_entry()
    {
        list test = [2.453, 1.345, 65.432, 23.433];
        test = llListSort(test, 1, TRUE);

        llOwnerSay(llList2CSV(test));
        // 1.345000, 2.453000, 23.433001, 65.431999
    }
}

 

Edited by Wulfie Reanimator
Link to comment
Share on other sites

1 hour ago, Wulfie Reanimator said:

In what cases does this not produce a correct result?


default
{
    state_entry()
    {
        list test = [2.453, 1.345, 65.432, 23.433];
        test = llListSort(test, 1, TRUE);

        llOwnerSay(llList2CSV(test));
        // 1.345000, 2.453000, 23.433001, 65.431999
    }
}

 

try putting 7.453 as first number of the list

result is Object: 7.453, 1.345, 65.432, 23.433

Link to comment
Share on other sites

default
{
    state_entry()
    {
        list test = [7.453, 2.453, 1.345, 65.432, 23.433];
        test = llListSort(test, 1, TRUE);

        llOwnerSay(llList2CSV(test));
    }
}

gave 1.345000, 2.453000, 7.453000, 23.433001, 65.431999

 

BUT...

 

default
{
    state_entry()
    {
        list test = ["7.453", 2.453, 1.345, 65.432, 23.433];
        test = llListSort(test, 1, TRUE);

        llOwnerSay(llList2CSV(test));
    }
}

Gave 7.453, 1.345000, 2.453000, 23.433001, 65.431999

 

At this point therefore I think you're going to have to paste your script, otherwise your assertions are not very well supported and somebody might get hurt when they topple to the ground

Edited by Profaitchikenz Haiku
  • Thanks 1
Link to comment
Share on other sites

forgive the mess

time is a string in xx.xxx format

list besttime = besttime+[time];
sort();

 

string sort()
{
    string bestlap;
    list best = llListSort( besttime, 1, TRUE );    
    string primo = (llList2String(best,0));
    timelist = llParseString2List(primo,["."],[""]);
    integer secondi = (llList2Integer(timelist,0));
    integer decimi = (llList2Integer(timelist,1));    
    string parse = getTime(secondi);  //this is another user function converting seconds in minute.seconds
    bestlap=parse+"."+(string)decimi;        
    return bestlap;  //mm.ss.ddd
}

i've seen not always i get the best time from the list, surely there is something wrong in my code

Link to comment
Share on other sites

9 hours ago, Xed Saunders said:

forgive the mess

time is a string in xx.xxx format


list besttime = besttime+[time];
sort();

string sort()
{
    string bestlap;
    list best = llListSort( besttime, 1, TRUE );
    string primo = (llList2String(best,0));
    timelist = llParseString2List(primo,["."],[""]);
    integer secondi = (llList2Integer(timelist,0));
    integer decimi = (llList2Integer(timelist,1));
    string parse = getTime(secondi);  //this is another user function converting seconds in minute.seconds
    bestlap=parse+"."+(string)decimi;
    return bestlap;  //mm.ss.ddd
}

i've seen not always i get the best time from the list, surely there is something wrong in my code

Well, based on this, "list besttime" will only have one value and there is nothing to sort.
Edit: I had some sleep and realized that you are actually correctly adding to the list, so it does have multiple values.

I recommend adding some debug messages to see what's going on.

list besttime = besttime+[time];
sort();

string sort()
{
    string bestlap;

    llOwnerSay(llList2CSV(besttime)); // debug
    list best = llListSort( besttime, 1, TRUE );
    llOwnerSay(llList2CSV(best)); // debug

    string primo = (llList2String(best,0));
    timelist = llParseString2List(primo,["."],[""]);
    integer secondi = (llList2Integer(timelist,0));
    integer decimi = (llList2Integer(timelist,1));
    string parse = getTime(secondi);  //this is another user function converting seconds in minute.seconds
    bestlap=parse+"."+(string)decimi;
    return bestlap;  //mm.ss.ddd
}

 

Edited by Wulfie Reanimator
  • Like 1
Link to comment
Share on other sites

You can not sort a list of strings that contain floats. Example: "23.123" is smaller than "7.456" of course. It's strings and not numbers!

I see 3 ways to get that going:

1 - normalize the strings: "23.123" stays and "7.123" becomes "07.123"

2 - use floating points: but you probably need some clever rounding since 23.123 can easily become 23.12299999999 for example

3 - use integers: 23.123 becomes 23*1000 + 123 = 23123
to reverse the process: 23123 / 1000 = 23   and    23123 % 1000 = 123

I would use integers - easy and quick to implement.

Link to comment
Share on other sites

floats stored as floats in a list will sort as floats

@Xed. Look at these lines in your code:

 

time is a string in xx.xxx format

list besttime = besttime+[time];

list best = llListSort( besttime, 1, TRUE ); 

[time] is a string. So "best" is a list of sorted strings

try casting "time" as a float when you add it. e.g

besttime = besttime + [ (float)time ];

 

Link to comment
Share on other sites

6 hours ago, Xed Saunders said:

if you are sure of this statement this seem to me the easiest solution

Prof showed the proof of this in a post above.  Another proof

default
{
   state_entry()
   {
       list decimals;
       integer i;
       for (i = 0; i < 10; i++)
       {
          float f = llFrand(20.0);
          decimals += [f];
       }
    
       llSay(0, llList2CSV(decimals) + "\n" + llList2CSV(llListSort(decimals, 1, TRUE)));
   }
}

 

  • Thanks 1
Link to comment
Share on other sites

yes, it suit my needs, it works with time.here is an unuseful program to test it

case closed.

list times;
list sorted;
default {
    state_entry()
    {
        llResetTime();
    }
    touch_start(integer num_touch)
    {
        float time = llGetTime();
        llResetTime();
        times = times+[time];
        llSay(0,(string)time + " seconds have elapsed since the last touch." );
        sorted = llListSort(times,1,TRUE);
        string tmp = llDumpList2String(sorted,"*");
        llOwnerSay("sorted:"+tmp);
    }
}

  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1799 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...