Ruby:如何对字符串进行排序,保留一些字符?

时间:2016-12-01 13:31:52

标签: ruby

我是Ruby的新手,我想对字符串进行排序,但保留非字母数字字符。 例如:render() { let video = require('../videos/HISTORISCHETUIN.mp4'); return ( <View> <TouchableHighlight onPress={console.log("Test")}> <View> <Video source={video} // Can be a URL or a local file. ref={ref => this.player = ref} // Store reference rate={1.0} // 0 is paused, 1 is normal. volume={1.0} // 0 is muted, 1 is normal. muted={false} // Mutes the audio entirely. paused={true} // Pauses playback entirely. resizeMode="stretch" // Fill the whole screen at aspect ratio. repeat={true} // Repeat forever. playInBackground={false} // Audio continues to play when app entering background. playWhenInactive={false} // [iOS] Video continues to play when control or notification center are shown. progressUpdateInterval={250.0} // [iOS] Interval to fire onProgress (default to ~250ms) onLoadStart={this.loadStart} // Callback when video starts to load onLoad={() => { this.player.seek(30); }} // Callback when video loads onProgress={this.setTime} // Callback every ~250ms with currentTime onEnd={console.log("Test")} // Callback when playback finishes onError={this.videoError} // Callback when video cannot be loaded style={styles.video} /> </View> </TouchableHighlight> </View> )

我尝试过:

console.log()

但我无法弄清楚如何将非字母数字字符放回去。

1 个答案:

答案 0 :(得分:2)

这是对问题的回答:&#34; ...我想对字符串进行排序,但保留非字母数字字符。&#34;直到我更仔细地阅读你的例子,我才意识到这不是你想要的。我会留下我的答案,因为它确实回答了陈述的问题。

<强>代码

def sort_alphas(str)
  alphas, non_alphas = str.each_char.with_index.partition { |c,_| c =~ /[[:alpha:]]/ }
  chrs, offsets = alphas.transpose
  chrs.sort.zip(offsets).concat(non_alphas).sort_by(&:last).map(&:first).join
end

示例

str = "hello, sally! seen 10/dec/2016"
sort_alphas(str)
  #=> "acdee, eehll! llno 10/ssy/2016"

<强>解释

示例字符串的步骤如下。

alphas, non_alphas = str.each_char.with_index.partition { |c,_|
  c =~ /[[:alpha:]]/ }
alphas
  #=> [["h", 0], ["e", 1], ["l", 2], ["l", 3], ["o", 4], ["s", 7], ["a", 8],
  #    ["l", 9], ["l", 10], ["y", 11], ["s", 14], ["e", 15], ["e", 16],
  #    ["n", 17], ["d", 22], ["e", 23], ["c", 24]]
non_alphas
  #=> [[",", 5], [" ", 6], ["!", 12], [" ", 13], [" ", 18], ["1", 19], ["0", 20],
  #    ["/", 21], ["/", 25], ["2", 26], ["0", 27], ["1", 28], ["6", 29]] 
chrs, offsets = alphas.transpose
chrs
  #=> ["h", "e", "l", "l", "o", "s", "a", "l", "l", "y", "s", "e", "e", "n",
  #    "d", "e", "c"] 
offsets
  #=> [0, 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 16, 17, 22, 23, 24]

sorted_alphas = chrs.sort.zip(offsets)
  # => [["a", 0], ["c", 1], ["d", 2], ["e", 3], ["e", 4], ["e", 7], ["e", 8],
  #     ["h", 9], ["l", 10], ["l", 11], ["l", 14], ["l", 15], ["n", 16],
  #     ["o", 17], ["s", 22], ["s", 23], ["y", 24]] 
sorted_arr = sorted_alphas.concat(non_alphas)
  #=> [["a", 0], ["c", 1], ["d", 2], ["e", 3], ["e", 4], ["e", 7], ["e", 8],
  #    ["h", 9], ["l", 10], ["l", 11], ["l", 14], ["l", 15], ["n", 16], ["o", 17],
  #    ["s", 22], ["s", 23], ["y", 24], [",", 5], [" ", 6], ["!", 12], [" ", 13], 
  #    [" ", 18], ["1", 19], ["0", 20], ["/", 21], ["/", 25], ["2", 26], ["0", 27],
  #    ["1", 28], ["6", 29]] 
ordered_arr = sorted_arr.sort_by(&:last)
  #=> [["a", 0], ["c", 1], ["d", 2], ["e", 3], ["e", 4], [",", 5], [" ", 6],
  #    ["e", 7], ["e", 8], ["h", 9], ["l", 10], ["l", 11], ["!", 12], [" ", 13],
  #    ["l", 14], ["l", 15], ["n", 16], ["o", 17], [" ", 18], ["1", 19], ["0", 20],
  #    ["/", 21], ["s", 22], ["s", 23], ["y", 24], ["/", 25], ["2", 26], ["0", 27],
  #    ["1", 28], ["6", 29]] 
ordered_chrs = ordered_arr.map(&:first)
  #=> ["a", "c", "d", "e", "e", ",", " ", "e", "e", "h", "l", "l", "!", " ", "l",
  #    "l", "n", "o", " ", "1", "0", "/", "s", "s", "y", "/", "2", "0", "1", "6"] 
ordered_chrs.join
  #=> "acdee, eehll! llno 10/ssy/2016"