The International Simutrans Forum

Development => Patches & Projects => Incorporated Patches and Solved Bug Reports => Topic started by: jk271 on May 18, 2014, 10:08:52 AM

Title: Alphabetically sorted list of pak sets
Post by: jk271 on May 18, 2014, 10:08:52 AM
If you start simutrans with multiple pak sets, a dialog for pak set selection appears. The pak sets are displayed randomly from my point of view (I have not read throught the corresponding source code).

It would be nice to have pak sets sorted in some way, e.g. alpabetically. The user would find the desired pak set easily from the menu.

I have several versions of one pak set for testing purposes and it is difficult do find the required one. See the attached screenshot.

The screenshot is from current development version of simutrans - revision 7218.
Title: Re: Alphabetically sorted list of pak sets
Post by: Ters on May 18, 2014, 12:28:37 PM
It's probably listed the way they are stored, which in practice will be more or less in the order they were installed.
Title: Re: Alphabetically sorted list of pak sets
Post by: Leartin on May 18, 2014, 12:30:24 PM
I'm using windows and it is sorted alphabetically, regardless of the installation date.
Title: Re: Alphabetically sorted list of pak sets
Post by: An_dz on May 18, 2014, 01:56:29 PM
It is sorted alphabetically, but not alpha-numerically. Just need to include the code to sort numbers.

See that pak128 comes first from pak64 because 1<6.

At least on my installations is like this.


Edit:
I'm with Ters, probably as how they are stored/loaded.
Title: Re: Alphabetically sorted list of pak sets
Post by: jk271 on May 18, 2014, 02:06:26 PM
Quote from: Leartin on May 18, 2014, 12:30:24 PM
I'm using windows and it is sorted alphabetically, regardless of the installation date.

I am using Linux. (32-bit Debian 7.5) It sound that it is OS-dependent.
Title: Re: Alphabetically sorted list of pak sets
Post by: Ters on May 18, 2014, 06:26:10 PM
Quote from: jk271 on May 18, 2014, 02:06:26 PM
I am using Linux. (32-bit Debian 7.5) It sound that it is OS-dependent.

Actually, it's typically file system dependent.
Title: Re: Alphabetically sorted list of pak sets
Post by: jk271 on June 20, 2014, 07:17:09 PM
Problem solved.
I have created a patch sorting alphabetically the items in the pak selector menu. The patch is in the attachech file.

I ask moderator to be so kind and move this discussion to the patch releases.


Title: Re: Alphabetically sorted list of pak sets
Post by: An_dz on June 20, 2014, 09:45:02 PM
Great, you added in slist_tpl.h so it may be used in other places. Haven't tested yet but thanks a lot.
Title: Re: Alphabetically sorted list of pak sets
Post by: prissi on June 21, 2014, 09:53:13 PM
As An_dz says, good you added it to the template. In in r7254. Thank you.
Title: Re: Alphabetically sorted list of pak sets
Post by: jk271 on June 23, 2014, 07:35:02 PM
Thank you for fast applying of the patch. Having the code in trunk (svn) / master (git) help me lot with testing of generated valleys.
Title: Re: Alphabetically sorted list of pak sets
Post by: An_dz on June 24, 2014, 06:03:26 PM
I took the liberty to extend this code to do the sorting alpha-numerically and case insensitive.

What do you think? Can this be in trunk?
Title: Re: Alphabetically sorted list of pak sets
Post by: prissi on June 24, 2014, 08:08:40 PM
personally I would do only do:

int l_diff = atoi(left)-atoi(right);
if(  diff==0  ) {
return strcmp( tolower(left), tolower(right) );
}
return diff;


to get essentially the same order. Still tolower does not work correctly on any non-ASCII stufff.
Title: Re: Alphabetically sorted list of pak sets
Post by: An_dz on June 24, 2014, 09:29:02 PM
I don't get how this works, tolower is for single character, atoi doesn't work if first char is not a number.

I could reduce to this with your idea:
if (( NULL != l.info ) && ( NULL != r.info )) {
const char *linfo = l.info;
const char *rinfo = r.info;
char left;
char right;
while (  (left = *linfo)  &&  (right = *rinfo)  ) {
bool left_is_number = left>='0' && left<='9';
bool right_is_number = right>='0' && right<='9';
if (  left_is_number  &&  right_is_number  ) {
int diff = atoi(linfo) - atoi(rinfo);
if(  diff != 0  ) {
return diff;
}
}
else {
const int diff = tolower(left) - tolower(right);
if (diff != 0) {
return diff;
}
}
++linfo;
++rinfo;
}
}
Title: Re: Alphabetically sorted list of pak sets
Post by: prissi on June 25, 2014, 09:32:33 AM
If the first character is not a number, then strcmp would return the correct result as well. But you are right, I should use stricmp (when MSC_VER) or strcasecmp (otherwise). And you had a problem with identical strings, there you need to return zero. Anyway, how about:

const char *left = l.info;
const char *right = r.info;
while(  *left  &&  *right  ) {
int diff = atoi(left)-atoi(right);
if(  diff==0  ) {
  diff = tolower(left)-tolower(right);
}
if(  diff  ) {
return diff;
}
left++;
right++;
}
// for equal strings we could use memory location ... (int)(left-right)
return 0;
Title: Re: Alphabetically sorted list of pak sets
Post by: Ters on June 25, 2014, 05:26:59 PM
It doesn't seem to me that that handles strings of different length very well.

Another thing might be to use strtol instead of atoi to compare and skip whole sequences of digits in one go. It might give a better result when comparing "+1b" and "1a", but the result can be somewhat unexpected either way.
Title: Re: Alphabetically sorted list of pak sets
Post by: An_dz on June 25, 2014, 06:47:23 PM
There's no problem with identical string, there already is a return 0 after this if in the original code by jk271.

Edit: Your code works, but has to be diff = tolower(*left) - tolower(*right);, or you'll be getting the string and not the char.

I don't get why to use strtoul Ters, atoi seems to work with your example.