How to sort a list alphabetically but ignore numbers?

In summary, the conversation discusses different methods for sorting a list of URLs using only the letters in the URL. Suggestions include using the / as a separator and extracting the third field to sort on, using the Right$(,) function, and using a Python script. The conversation also mentions the need to provide a file of URLs to be sorted when using the Python script.
  • #1
rollcast
408
0
I have a list of urls I need to sort but I need to sort them only using the letters in the url?

Eg.

example.com/174353/a
example.com/3452344/c
example.com/435322/d
example.com/2342/b
example.com/23456/e

Would be sorted as:

example.com/174353/a
example.com/2342/b
example.com/3452344/c
example.com/435322/d
example.com/23456/e

Thanks
AL
 
Computer science news on Phys.org
  • #2
if you process the file using the / as a separator then you can extract out the 3rd field to sort on.

you could probably use awk to do the parsing to write a new file containing the key then url on one line
then use the sort command to generate a sorted list.

thats if you're running on linux or some unix variant.
 
  • #3
Most varieties of BASIC have a Right$(,) function, which would read the closing letter(s) off the target string.
 
  • #4
jedishrfu said:
if you process the file using the / as a separator then you can extract out the 3rd field to sort on.

you could probably use awk to do the parsing to write a new file containing the key then url on one line
then use the sort command to generate a sorted list.

thats if you're running on linux or some unix variant.

I'm on Ubuntu 11.10.

The addresses aren't as simple as I thought here's one for example.

http:// www. flyforums. co.uk/fly-tying-step-step/160-beacon-beige.html

(The spaces are just there to stop it hyperlinking it.)

They all follow the same pattern with a number-couple of keywords separated by -s .html
 
  • #5
how about this python script

http://snippets.dzone.com/posts/show/10115
 
Last edited by a moderator:
  • #6
jedishrfu said:
how about this python script

http://snippets.dzone.com/posts/show/10115

It comes up with an error,

" File "sort.py", line 3, in <module>
filename = sys.argv[1]
IndexError: list index out of range"
 
Last edited by a moderator:
  • #7
Please clarify. Would the URL

http:// www. flyforums. co.uk/fly-tying-step-step/160-beacon-beige.html

be lexicographically sorted with the value

httpwwwflyformscoukflytryingstepstepbeaconbeigehtml

?
 
  • #8
when you run it you're supposed to provide a file of your urls to be sorted.

./sorturls.py myfileofurls.lst

where sorturls.py is the name of the script and the myfileofurls.lst is your file of unsorted urls.
 

1. How do I sort a list alphabetically while ignoring numbers?

To sort a list alphabetically while ignoring numbers, you can use a sorting algorithm that takes into account the ASCII values of the characters in the list. This way, numbers will be treated as characters and will not affect the alphabetical sorting.

2. Can I use the built-in sort function to ignore numbers?

No, most built-in sort functions will not ignore numbers and will sort them as if they were regular characters. You will need to implement a custom sorting algorithm or use a library that offers this functionality.

3. How can I make sure that numbers are ignored while sorting a list?

You can use a regular expression or a string parsing function to check if a character is a number or not. If it is a number, you can remove it from the list before sorting. Alternatively, you can use a sorting algorithm that takes into account the ASCII values of the characters, as mentioned before.

4. What if my list contains both numbers and letters, but I only want to sort the letters?

In this case, you can use a filtering function to remove the numbers from the list before sorting. You can also use a regular expression to extract only the letters from the list and sort them separately.

5. Can I sort a list alphabetically while ignoring special characters?

Yes, you can use a regular expression or a string parsing function to remove the special characters from the list before sorting. You can also use a library that offers this functionality, as it may be more efficient and handle edge cases better.

Similar threads

  • Computing and Technology
2
Replies
41
Views
4K
  • Computing and Technology
Replies
5
Views
879
Replies
38
Views
3K
Replies
2
Views
442
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
909
  • Computing and Technology
Replies
4
Views
762
  • Computing and Technology
2
Replies
44
Views
3K
  • Programming and Computer Science
Replies
21
Views
519
Replies
23
Views
3K
  • Computing and Technology
Replies
15
Views
1K
Back
Top