AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   General (https://forums.alliedmods.net/forumdisplay.php?f=7)
-   -   Questions and suggestions about ArrayFindString and sorting natives (https://forums.alliedmods.net/showthread.php?t=310440)

shauli 09-03-2018 16:41

Questions and suggestions about ArrayFindString and sorting natives
 
Hi, have a few questions about the sort natives and ArrayFindString native:

1. Recently I came across a problem with sorting an array that has a lot of equal items. After the sorting, the items are being "shifted" down for some reason and I don't know if it's supposed to be like this. Here's an example code:
Spoiler

Result:
Quote:

0 | 1
1 | 2
2 | 3
3 | 4
4 | 0
The result is the same when I tried it on SortCustom1D, ArraySort or ArraySortEx.
If all the items are equal (according to my sort algorithm), so why does the index change? shouldn't it stay the exact same?

Also -- on a side note, would be really cool to have a cmp(x,y) native/stock inside a default include file, just like floatcmp has one.

2. ArrayFindString checking contain instead of equal and is case sensitive. Basically this makes this native useless 90% of the the times, it's to dangerous to use it if it's acting like 'contain' (instead of 'equal') and it's not very practical. If I have an array of SteamIDS, I might get STEAM_0:111156 when searching for STEAM_0:1111. If I have an array of maps I might get de_dust2 when searching for de_dust. Would be very nice to have a parameter for case insensitive/case sensitive and maybe a param/second native if to act like 'contain' or 'equal'.

Thank you for keeping this project alive, everyone.

HamletEagle 09-04-2018 03:50

Re: Questions and suggestions about ArrayFindString and sorting natives
 
SortCustom1D(and other natives probably) uses qsort to perform the actual sorting.
http://www.cplusplus.com/reference/cstdlib/qsort/
Documentation says the order of equal elements is undefined, this is why.

shauli 09-05-2018 11:52

Re: Questions and suggestions about ArrayFindString and sorting natives
 
Quote:

Originally Posted by HamletEagle (Post 2613615)
SortCustom1D(and other natives probably) uses qsort to perform the actual sorting.
http://www.cplusplus.com/reference/cstdlib/qsort/
Documentation says the order of equal elements is undefined, this is why.

Thanks, that explains it.
So basically if i'll do 'return 1' for equal items it will work as I expected.

Arkshine 09-05-2018 13:55

Re: Questions and suggestions about ArrayFindString and sorting natives
 
You are supposed to compare the elements.

Code:
public sort(elem1, elem2) {     if (elem1 < elem2)   return -1;     if (elem1 == elem2) return 0;     if (elem1 > elem2) return 1; }

shauli 09-05-2018 14:54

Re: Questions and suggestions about ArrayFindString and sorting natives
 
Quote:

Originally Posted by Arkshine (Post 2613857)
You are supposed to compare the elements.

Code:
public sort(elem1, elem2) {     if (elem1 < elem2)   return -1;     if (elem1 == elem2) return 0;     if (elem1 > elem2) return 1; }

That's if I have an array of integers. This was just an example for the post. Lets say I have an array of SteamIDs + points to each key, if 2 SteamIDs have the same amount of points their index will still change.

Some comments on my 2nd suggestion? :)


All times are GMT -4. The time now is 09:05.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.