用字母排序数字字符串

时间:2016-11-08 09:53:01

标签: mysql sql sorting

我有

之类的字符串
NSMutableParagraphStyle *ParagraphStyle = [[NSMutableParagraphStyle alloc] init];
    ParagraphStyle.alignment = NSTextAlignmentJustified;
    ParagraphStyle.firstLineHeadIndent = 0.001;
    ParagraphStyle.lineSpacing = 5.0;
   NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:labeltext attributes:@{NSParagraphStyleAttributeName:ParagraphStyle}];
self.labelDetails.AttributedText = attStr;

当我想按此顺序排序时:

70x100 CM
70x140 CM
70x70 CM
72x120 CM
70x130x70 CM
70x75x70 CM
72x72 CM

这是我的代码:

70x70 CM
70x75x70 CM
70x100 CM
70x130x70 CM
70x140 CM
72x72 CM
72x120 CM

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

草莓的答案的一个小模式。这会将'cm'更改为一系列x0,以确保排序顺序总是有3个维度

SELECT *
FROM eaov
ORDER BY CAST(SUBSTRING_INDEX(REPLACE(eaov.value, ' CM', 'x0x0x0'), 'x', 1) as UNSIGNED INTEGER),
        CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(eaov.value, ' CM', 'x0x0x0'), 'x', 2), 'x', -1) as UNSIGNED INTEGER),
        CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(eaov.value, ' CM', 'x0x0x0'), 'x', 2), 'x', -1) as UNSIGNED INTEGER)

表现不是它的强项。

答案 1 :(得分:1)

我并不是认真地提倡这个解决方案 - 而是作为一项临时措施(并且很容易适应规范化设计的目的)......

SELECT * FROM my_table
  ORDER 
     BY substring_index(string,'x',1) + 0 
      , substring_index(substring_index(string,'x',-1),' ',1) + 0