Raised This Month: $12 Target: $400
 3% 

Questions and suggestions about ArrayFindString and sorting natives


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shauli
Member
Join Date: Jun 2018
Old 09-03-2018 , 16:41   Questions and suggestions about ArrayFindString and sorting natives
Reply With Quote #1

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.
shauli is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 09-04-2018 , 03:50   Re: Questions and suggestions about ArrayFindString and sorting natives
Reply With Quote #2

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.
__________________

Last edited by HamletEagle; 09-04-2018 at 03:50.
HamletEagle is offline
shauli
Member
Join Date: Jun 2018
Old 09-05-2018 , 11:52   Re: Questions and suggestions about ArrayFindString and sorting natives
Reply With Quote #3

Quote:
Originally Posted by HamletEagle View Post
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.
shauli is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 09-05-2018 , 13:55   Re: Questions and suggestions about ArrayFindString and sorting natives
Reply With Quote #4

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; }
__________________
Arkshine is offline
shauli
Member
Join Date: Jun 2018
Old 09-05-2018 , 14:54   Re: Questions and suggestions about ArrayFindString and sorting natives
Reply With Quote #5

Quote:
Originally Posted by Arkshine View Post
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?

Last edited by shauli; 09-05-2018 at 14:54.
shauli is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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