某些函数可以对字符串或[]字节进行排序:
"bcad" to "abcd"
or
[]byte("bcad") to []byte("abcd")
字符串只有字母。
如果是字母和数字?
我找到了排序包但不是想要的功能。
感谢。
答案 0 :(得分:28)
为每个角色创建一个字符串只为Join
而感到浪费。
这里的浪费少一点,但锅炉板更多。 playground://XEckr_rpr8
type sortRunes []rune
func (s sortRunes) Less(i, j int) bool {
return s[i] < s[j]
}
func (s sortRunes) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortRunes) Len() int {
return len(s)
}
func SortString(s string) string {
r := []rune(s)
sort.Sort(sortRunes(r))
return string(r)
}
func main() {
w1 := "bcad"
w2 := SortString(w1)
fmt.Println(w1)
fmt.Println(w2)
}
答案 1 :(得分:16)
您可以将字符串转换为字符串切片,对其进行排序并将其转换回字符串:
package main
import (
"fmt"
"sort"
"strings"
)
func SortString(w string) string {
s := strings.Split(w, "")
sort.Strings(s)
return strings.Join(s, "")
}
func main() {
w1 := "bcad"
w2 := SortString(w1)
fmt.Println(w1)
fmt.Println(w2)
}
打印:
bcad
abcd
答案 2 :(得分:3)
利用功能this.api.get_someapi()
.subscribe(data => {
this.recent_event_data = data;
console.log('recent event data', this.recent_event_data);
var length = this.recent_event_data.length;
for(var i = 0; i < length; i++){
var lati = this.recent_event_data[i].latitude;
var longi = this.recent_event_data[i].longitude;
this.flash_type = this.recent_event_data[i].flash_type;
for(var j = 0; j< this.latitude.length; j++){
console.log('loop long',this.longitude[j]);
console.log('loop lat', this.latitude[j]);
var marker = new ol.Feature({
geometry: new ol.geom.Point(
ol.proj.fromLonLat([parseFloat(this.longitude[j]), parseFloat(this.latitude[j])])),
});
marker.setStyle(new ol.style.Style({
image: new ol.style.Icon(({
// color: '#ffcd46',
crossOrigin: 'anonymous',
src: 'https://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png'
// src:'/assets/imgs/blue-dot.png'
}))
}));
var vectorSource = new ol.source.Vector({
features: [marker]
});
var markerVectorLayer = new ol.layer.Vector({
source: vectorSource,
});
map.addLayer(markerVectorLayer);
}
}
});
this.loading.dismiss();
// Creating District Vector Source
var district = new ol.layer.Vector({
renderMode: 'image',
// title:"District",
visible:true,
// baseLayer: true,
source: new ol.source.Vector({
url: 'somemap',
format: new ol.format.GeoJSON()
})
});
// OSM Layer
var osm=new ol.layer.Tile({
source: new ol.source.OSM(),
// title:'Map',
// type:'base',
// baseLayer: true,
visible:true,
});
// Adding the District and OSM layer to the map
var map = new ol.Map({
layers: [osm, district],
renderer: 'canvas',
target: 'map', // Rendering to a particular ID
view: new ol.View({
center: ol.proj.transform([84.3995, 20.9517], 'EPSG:4326', 'EPSG:3857'),
zoom: 6.3
}),
controls: ol.control.defaults().extend([
new ol.control.FullScreen()
]),
});
var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
/**
* Create an overlay to anchor the popup to the map.
*/
var overlay = new ol.Overlay({
element: container,
autoPan: true,
});
closer.onclick = function() {
overlay.setPosition(undefined);
closer.blur();
return false;}
map.on("singleclick", function(evt) {
//var coordinate = evt.coordinate;
//var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(
//coordinate, 'EPSG:3857', 'EPSG:4326'));
content.innerHTML = '<p>You clicked here:</p><code>';
//overlay.setPosition(coordinate);
});
有一种简单的方法:
sort.Slice
答案 3 :(得分:0)
package main
import (
"fmt"
"sort"
)
func main() {
word := "1àha漢字Pépy5"
charArray := []rune(word)
sort.Slice(charArray, func(i int, j int) bool {
return charArray[i] < charArray[j]
})
fmt.Println(string(charArray))
}
输出:
15Pahpyàé字漢