我有一个像这样的数组:
"Turkish 1"
"Indonesian 1"
"Malay 2"
"Indonesian 3"
"Yiddish 1"
"Urdu 2"
"Malay 1"
"Indonesian 2"
"Urdu 1"
我想这样排序:
"Urdu 1"
"Urdu 2"
"Indonesian 1"
"Indonesian 2"
"Indonesian 3"
"Malay 1"
"Malay 2"
"Turkish 1"
"Yiddish 1"
请注意,从“ U”开始的项目在顶部,其余部分按字母顺序排序。如何使用Java或Kotlin中的某种排序算法来实现此目的?
现在,我只知道遍历列表并将项目从U开始放置到列表顶部。
另外,即使在Sqlite中也可以,也请分享。
谢谢。
答案 0 :(得分:2)
应用高级排序覆盖的最佳方法是从覆盖条件中得出一个整数值,然后按该值进行排序。
在Java中:
var adBook = (function () {
// default fields for understanding
var pplDetails = [{
firstName: 'Sam',
lastName: 'Smith',
phone: '004477995544',
address: '33 jump st, London'
}];
//variables declaration
var table = $('#table1');
var $tbody = table.find('tbody');
var $firstName = table.find('#firstName');
var $lastName = table.find('#lastName');
var $phone = table.find('#phone');
var $address = table.find('#address');
var $button = table.find('#add');
var $btnRemove = table.find('#remove');
var $input = table.find(".edit");
//events calls
$button.on('click', adPerson);
table.on('click', '#remove', delPerson);
console.log($input);
build();
//function to create new table data
function build() {
$tbody.html('');
var length = pplDetails.length;
for (var i = 0; i < length; i++) {
table.prepend('<tr><td><input type="text" value="' + pplDetails[i].firstName + '" ></td> <td><input type="text" value="' + pplDetails[i].lastName + '" ></td> <td><input type="text" value="' + pplDetails[i].phone + '" ></td> <td><input type="text" value="' + pplDetails[i].address + '" ></td> <td> <button id="remove" class="btn btn-block">Del</button></td></tr>');
}
}
//function to add persons details
function adPerson() {
var data = {
firstName: $firstName.val(),
lastName: $lastName.val(),
phone: $phone.val(),
address: $address.val()
};
pplDetails.push(data);
$firstName.val('');
$lastName.val('');
$phone.val('');
build()
}
//function to delete details
function delPerson(event) {
var element = event.target.closest('tr');
var i = table.find('td').index(element);
pplDetails.splice(i, 1);
build();
}
return {
adPerson: adPerson,
delPerson: delPerson
};
})();
//Search bar
$(document).ready(function () {
$("#myInput").on("keyup", function () {
var value = $(this).val().toLowerCase();
$('tbody tr').filter(function () {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
输出
String[] strings = {
"Turkish 1",
"Indonesian 1",
"Malay 2",
"Indonesian 3",
"Yiddish 1",
"Urdu 2",
"Malay 1",
"Indonesian 2",
"Urdu 1" };
Arrays.sort(strings, Comparator.comparingInt((String s) -> s.startsWith("U") ? 0 : 1)
.thenComparing(Comparator.naturalOrder()));
System.out.println(Arrays.toString(strings));
在SQL中:
[Urdu 1, Urdu 2, Indonesian 1, Indonesian 2, Indonesian 3, Malay 1, Malay 2, Turkish 1, Yiddish 1]
答案 1 :(得分:0)
在Kotlin中,一种方法是将其分为两个列表,对每个列表进行排序,然后将它们加入:
val strings = listOf("Turkish 1", "Indonesian 1", "Malay 2", "Indonesian 3",
"Yiddish 1", "Urdu 2", "Malay 1", "Indonesian 2", "Urdu 1")
val result = strings.partition{ it.startsWith("U") }
.run{ first.sorted() + second.sorted() }
println(result)