如何在安卓中将数组按星期几的格式排序

huangapple go评论154阅读模式
英文:

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(&quot;weekday&quot;)
	val weekday: String,

	@field:SerializedName(&quot;id&quot;)
	val id: String
)

private val Weekday.index: Int
	get() = when (weekday) {
		&quot;Monday&quot; -&gt; 0
		&quot;Tuesday&quot; -&gt; 1
		&quot;Wednesday&quot; -&gt; 2
		&quot;Thursday&quot; -&gt; 3
		&quot;Friday&quot; -&gt; 4
		&quot;Saturday&quot; -&gt; 5
		&quot;Sunday&quot; -&gt; 6
		else -&gt; 7
	}

fun sort() {
	val weekdays: List&lt;Weekday&gt; = /* TODO */

	val result = weekdays.sortedWith(object : Comparator&lt;Weekday&gt; {
		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

https://stackoverflow.com/questions/43429998/how-to-sort-strings-containing-day-of-week-names-in-ascending-order

答案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&lt;String, Integer&gt; dayMap = new HashMap&lt;&gt;();
dayMap.put(&quot;Monday&quot;, 1);
dayMap.put(&quot;Tuesday&quot;, 2);
dayMap.put(&quot;Wednesday&quot;, 3);
dayMap.put(&quot;Thursday&quot;, 4);
dayMap.put(&quot;Friday&quot;, 5);
dayMap.put(&quot;Saturday&quot;, 6);
dayMap.put(&quot;Sunday&quot;, 7);
ArrayList&lt;Days&gt; weekdays = new ArrayList&lt;&gt;();
for (int i = 0; i &lt; daysArray.length(); i++) {
JSONObject dayObject = daysArray.getJSONObject(i);
Days day = new Days(dayObject.getString(&quot;id&quot;), dayObject.getString(&quot;weekday&quot;));
weekdays.add(day);
}
Collections.sort(weekdays, new Comparator&lt;Days&gt;() {
@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 = &quot;&quot;;
String day = &quot;&quot;;
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(&quot;id&quot;, id);
object.put(&quot;weekday&quot;, 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 }

huangapple
  • 本文由 发表于 2020年4月7日 02:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61066011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定