0
>>> x = set(['216', '217', '214', '215', '212', '213', '210', '211', '165', '264', '218', '219', '133', '132', '131', '130', '137', '136', '135', '226', '139', '138', '166', '24', '25', '26', '27', '20', '21', '22', '23', '95', '28', '29', '222', '288', '346', '4', '161', '8', '163', '348', '119', '868', '258', '120', '121', '122', '260', '124', '125', '126', '127', '128', '129', '167', '118', '59', '58', '55', '54', '57', '56', '51', '50', '53', '52', '298', '292', '293', '164', '201', '199', '179', '200', '195', '194', '706', '134', '191', '190', '193', '192', '115', '114', '88', '89', '111', '110', '113', '176', '82', '83', '80', '81', '86', '87', '84', '85', '117', '198', '3', '177', '171', '7', '247', '309', '225', '306', '307', '178', '244', '108', '109', '241', '240', '102', '103', '100', '101', '106', '107', '104', '105', '39', '38', '33', '32', '31', '30', '37', '36', '35', '34', '642', '181', '393', '205', '223', '207', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '175', '174', '173', '172', '254', '170', '203', '249', '248', '182', '183', '180', '2', '162', '187', '184', '6', '220', '188', '189', '197', '202', '313', '196', '221', '185', '99', '98', '168', '169', '229', '228', '91', '90', '93', '92', '160', '94', '97', '96', '11', '10', '13', '12', '15', '14', '17', '16', '19', '18', '272', '250', '116', '204', '151', '150', '153', '152', '155', '154', '157', '156', '159', '158', '112', '279', '234', '235', '236', '237', '231', '232', '224', '48', '49', '46', '47', '44', '45', '42', '43', '40', '41', '1', '320', '5', '9', '146', '147', '144', '145', '142', '143', '140', '141', '208', '394', '148', '149', '77', '76', '75', '74', '73', '72', '71', '70', '79', '78', '263', '262', '472', '123', '276'])
>>> type(x)

    <type 'set'>

Now I create a list

>>> y = list(x)
>>> type(y)
<type 'list'>

Now I sort

>>> y.sort()

>>> y

['1', '10', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '11', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '12', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '13', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '14', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '15', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '16', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '17', '170', '171', '172', '173', '174', '175', '176', '177', '178', '179', '18', '180', '181', '182', '183', '184', '185', '187', '188', '189', '19', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '2', '20', '200', '201', '202', '203', '204', '205', '207', '208', '21', '210', '211', '212', '213', '214', '215', '216', '217', '218', '219', '22', '220', '221', '222', '223', '224', '225', '226', '228', '229', '23', '231', '232', '234', '235', '236', '237', '24', '240', '241', '244', '247', '248', '249', '25', '250', '254', '258', '26', '260', '262', '263', '264', '27', '272', '276', '279', '28', '288', '29', '292', '293', '298', '3', '30', '306', '307', '309', '31', '313', '32', '320', '33', '34', '346', '348', '35', '36', '37', '38', '39', '393', '394', '4', '40', '41', '42', '43', '44', '45', '46', '47', '472', '48', '49', '5', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '6', '60', '61', '62', '63', '64', '642', '65', '66', '67', '68', '69', '7', '70', '706', '71', '72', '73', '74', '75', '76', '77', '78', '79', '8', '80', '81', '82', '83', '84', '85', '86', '868', '87', '88', '89', '9', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']

But check elements in y, they doesn't seem to be sorted. Whats wrong here?

3
  • 2
    That's because you are sorting strings not numbers Commented Jul 31, 2012 at 23:24
  • 1
    I got that, thank you for a quick spot check! Commented Jul 31, 2012 at 23:25
  • 2
    Being less snarky would help. Sorting strings is lexicographical, so '10' comes before '2'. See my answer if you don't want that. Commented Jul 31, 2012 at 23:25

2 Answers 2

16

If you want to sort them as ints, i.e. numerically, not lexicographically, use y = sorted(x, key=int).

Sign up to request clarification or add additional context in comments.

Comments

3

Because these are strings you are sorting e.g '1','2' and '100', they are ordered by the bytes that make up the string.. not the number the string represents.

Because the bytes that represent the character '1' is less that the byte that represents the character '2', all the strings starting with '1' will be first. etc etc.

You need do convert the strings to integers and then sort them to make them sort in numerical order.

G./

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.