-2

There is a array like that ---> [[text1, number1], [text2, number2]...]

I want to sort to this array(by first elements(texts)). This texts contains different characters such as ı, ç, ö... I found to location method but i couldn't use it. Also I want to describe own function.

  • How exactly do you define "alphabetically"? If you're given `ö ộ ȭ ò`, for example, what order are they supposed to come in? – Nick Reed Dec 31 '19 at 19:11
  • Since the letters are not defined in your desired "alphabetical" order, you need to define your own sort function, including the desired order. – Prune Dec 31 '19 at 19:18
  • A B C Ç D E F G Ğ H I İ J K L M N O Ö P R S Ş T U Ü V Y Z – cedricdef Dec 31 '19 at 19:18
  • Are there any sample code? I'm working on this for 2 days. I have a homework but I don't have time. – cedricdef Dec 31 '19 at 19:22

2 Answers2

0

Try using the sort method and specify the key as the first entry of every element in the list

import unicodedata 
def strip_accents(text):
    print("HIIIIIIII: %s"%text)
    return ''.join(char for char in
                   unicodedata.normalize('NFKD', text)
                   if unicodedata.category(char) != 'Mn')

l=[['ç', 2], ['ç', 10], ['a', 3], ['b', 1], ['d', 7]]
print(sorted(l, key=lambda k: strip_accents(k[0])))
print(l)
# [['a', 3], ['b', 1], ['ç', 2], ['ç', 10], ['d', 7]]

Refrences:

Syntax behind sorted(key=lambda: ...)

https://stackoverflow.com/a/4512721/8692977

Marsilinou Zaky
  • 1,038
  • 7
  • 17
0

Usually characters such as ç are alphabetically sorted after their non accentuated counterparts (c). In that sense, the encoding you are using may or may not help regarding sorting alphabetically.

I would suggest first defining your order relation:

[a, á, à, b, c, ç, ...]

Implementing a < compare function based on that relation, and using one of the many available sorting methods to sort your list.

ianmandarini
  • 395
  • 1
  • 15