-
Notifications
You must be signed in to change notification settings - Fork 192
Sorting string's characters according to their ASCII values #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hello @ChetanKarwa,
great to see you managed to build stdlib and get your new function working.
However, I am not totally convinced by the purpose of this function. The more common usage of a sort function would be to sort a list of strings (like the one in development in #311), say to output a sorted list of labels.
Do you have some usage cases that motivate a character sort function?
I implemented this because I've seen other languages supporting the sorting of a character array.
But they support a general sort function they don't have a separate sort function for the character array and that function is slow [time complexity - O(N*logN)]. So I thought of implementing something different for Fortran stdlib and so the one implemented in this PR is O(N).
Now giving a second thought to it currently I really can't figure out any use case of this function.
@wclodius2 Is your implementation related to this PR?
No, it isn't. My sort is of an array of elements of the string_type
and not of the characters of a string_type
scalar, or of a list of elements of the string_type
. FWIW I also view sorting of the characters as of very limited use, and sorting a list rather than an array would require aa drastic rewrite of my code.
I renamed this PR to avoid confusion with #408
What could be done is to provide the O(n) sorting algorithm of @ChetanKarwa as a specialization in #408 when the input is an array of character(len=1)
, but I don't expect this to be used very often.
The only usage I can think of currently is to find the median letter (and maybe the mode) in a character sequence.
Also, consider that this implementation is likely to fail for non-ASCII input:
print *, sort("Ångström") ! Fortran runtime error: Index '195' of dimension 1 of array 'count' above upper bound of 128
Uh oh!
There was an error while loading. Please reload this page.
I have implemented a sort function for a string. This function sorts the string's characters according to their ASCII values.
Eg:
string - "This is to be sorted"
sorted string - " Tbdeehiioorssstt"