Skip to content
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

Sorting string's characters according to their ASCII values #363

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

ChetanKarwa
Copy link

@ChetanKarwa ChetanKarwa commented Mar 26, 2021

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"

@ivan-pi
Copy link
Member

ivan-pi commented Mar 26, 2021

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?

@ChetanKarwa
Copy link
Author

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.

@awvwgk awvwgk added topic: utilities containers, strings, files, OS/environment integration, unit testing, assertions, logging, ... idea Proposition of an idea and opening an issue to discuss it reviewers needed This patch requires extra eyes labels Apr 17, 2021
@jvdp1
Copy link
Member

jvdp1 commented Apr 21, 2021

@wclodius2 Is your implementation related to this PR?

@wclodius2
Copy link
Contributor

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.

@jvdp1 jvdp1 changed the title Added sort to stdlib_string_type.f90 file Sorting string's characters according to their ASCII values May 24, 2021
@jvdp1
Copy link
Member

jvdp1 commented May 24, 2021

I renamed this PR to avoid confusion with #408

@ivan-pi
Copy link
Member

ivan-pi commented May 24, 2021

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.

@awvwgk
Copy link
Member

awvwgk commented May 24, 2021

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

@awvwgk awvwgk added waiting for OP This patch requires action from the OP and removed reviewers needed This patch requires extra eyes labels Jul 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea Proposition of an idea and opening an issue to discuss it topic: utilities containers, strings, files, OS/environment integration, unit testing, assertions, logging, ... waiting for OP This patch requires action from the OP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants