英文:
How to sort array to weekdays format android
问题
[
{
"id": "12",
"weekday": "Friday"
},
{
"id": "121",
"weekday": "Monday"
},
{
"id": "123",
"weekday": "Sunday"
},
{
"id": "222",
"weekday": "Tuesday"
}
]
现在我想要按照(星期一至星期日)的方式重新排列它,其中星期一首次出现,星期日最后出现。您该如何做到这一点?
英文:
I have below json :
[
{
"id" : "12",
"weekday": "Friday"
},
{
"id" : "121",
"weekday": "Monday"
},
{
"id" : "123",
"weekday": "Sunday"
},
{
"id" : "222",
"weekday": "Tuesday"
}
]
Now i want to re-arrange it in a manner(Mon-Sun) where monday comes first and sunday comes last.
How can i do that ?
答案1
得分: 3
这是使用 Kotlin 编写的示例代码,可以帮助你理解如何进行操作。你可以创建一个方法或属性,根据星期几为每个 JSON 模型分配一个值,然后基于该值进行排序。这个示例是区分大小写的,你也可以使其不区分大小写。
data class Weekday(
@field:SerializedName("weekday")
val weekday: String,
@field:SerializedName("id")
val id: String
)
private val Weekday.index: Int
get() = when (weekday) {
"Monday" -> 0
"Tuesday" -> 1
"Wednesday" -> 2
"Thursday" -> 3
"Friday" -> 4
"Saturday" -> 5
"Sunday" -> 6
else -> 7
}
fun sort() {
val weekdays: List<Weekday> = /* TODO */
val result = weekdays.sortedWith(object : Comparator<Weekday> {
override fun compare(o1: Weekday, o2: Weekday): Int {
return o1.index.compareTo(o2.index)
}
})
}
英文:
This is in Kotlin, but gives you an idea of what you can do. You can create a method or property that assigns a value to each JSON model based on the weekday and then sort based on that. This is case sensitive, you could make it case insensitive as well.
data class Weekday(
@field:SerializedName("weekday")
val weekday: String,
@field:SerializedName("id")
val id: String
)
private val Weekday.index: Int
get() = when (weekday) {
"Monday" -> 0
"Tuesday" -> 1
"Wednesday" -> 2
"Thursday" -> 3
"Friday" -> 4
"Saturday" -> 5
"Sunday" -> 6
else -> 7
}
fun sort() {
val weekdays: List<Weekday> = /* TODO */
val result = weekdays.sortedWith(object : Comparator<Weekday> {
override fun compare(o1: Weekday, o2: Weekday): Int {
return o1.index.compareTo(o2.index)
}
})
}
答案2
得分: 1
以下是翻译好的内容:
给工作日编号为0-6。然后根据这些编号进行排序。
这里有一个简单的示例:
英文:
Give numbers 0-6 to weekdays. Then sort them according to this numbers
There is a simple example here
答案3
得分: 1
请尝试以下代码:
public JSONArray sortWeekDays(JSONArray daysArray) {
JSONArray sortedWeekDays = new JSONArray();
HashMap<String, Integer> dayMap = new HashMap<>();
dayMap.put("Monday", 1);
dayMap.put("Tuesday", 2);
dayMap.put("Wednesday", 3);
dayMap.put("Thursday", 4);
dayMap.put("Friday", 5);
dayMap.put("Saturday", 6);
dayMap.put("Sunday", 7);
ArrayList<Days> weekdays = new ArrayList<>();
for (int i = 0; i < daysArray.length(); i++) {
JSONObject dayObject = daysArray.getJSONObject(i);
Days day = new Days(dayObject.getString("id"), dayObject.getString("weekday"));
weekdays.add(day);
}
Collections.sort(weekdays, new Comparator<Days>() {
@Override
public int compare(Days o1, Days o2) {
return dayMap.get(o1.getDay()).compareTo(dayMap.get(o2.getDay()));
}
});
for (Days days : weekdays) {
sortedWeekDays.put(days.getJsonObject());
}
return sortedWeekDays;
}
public class Days {
String id = "";
String day = "";
public Days(String id, String day) {
this.id = id;
this.day = day;
}
public String getDay() {
return day;
}
public JSONObject getJsonObject() {
super.toString();
JSONObject object = new JSONObject();
object.put("id", id);
object.put("weekday", day);
return object;
}
}
英文:
Try this
public JSONArray sortWeekDays(JSONArray daysArray) {
JSONArray sortedWeekDays = new JSONArray();
HashMap<String, Integer> dayMap = new HashMap<>();
dayMap.put("Monday", 1);
dayMap.put("Tuesday", 2);
dayMap.put("Wednesday", 3);
dayMap.put("Thursday", 4);
dayMap.put("Friday", 5);
dayMap.put("Saturday", 6);
dayMap.put("Sunday", 7);
ArrayList<Days> weekdays = new ArrayList<>();
for (int i = 0; i < daysArray.length(); i++) {
JSONObject dayObject = daysArray.getJSONObject(i);
Days day = new Days(dayObject.getString("id"), dayObject.getString("weekday"));
weekdays.add(day);
}
Collections.sort(weekdays, new Comparator<Days>() {
@Override
public int compare(Days o1, Days o2) {
return dayMap.get(o1.getDay()).compareTo(dayMap.get(o2.getDay()));
}
});
for (Days days : weekdays) {
sortedWeekDays.put(days.getJsonObject());
}
return sortedWeekDays;
}
public class Days {
String id = "";
String day = "";
public Days(String id, String day) {
this.id = id;
this.day = day;
}
public String getDay() {
return day;
}
public JSONObject getJsonObject() {
super.toString();
JSONObject object = new JSONObject();
object.put("id", id);
object.put("weekday", day);
return object;
}
}
答案4
得分: 0
你没有说明你期望的最终数据格式是什么。但是假设你已经将其转换为类似这样的List
:
data class Day(val id: Int, val name: String)
如果 JSON 中的星期几始终是英文,你可以像这样对它们进行排序:
val sortedList = list.sortedBy { DayOfWeek.valueOf(it.name.toUpperCase()).value }
英文:
You didn't say what your resulting data format should be. But supposing you've already converted it to a List
of something like this:
data class Day(val id: Int, val name: String)
if the JSON day of the week will always be in English, you can sort them like this:
val sortedList = list.sortedBy { DayOfWeek.valueOf(it.name.toUpperCase()).value }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论