You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin中两类字符串列表排序问题求助:基于首个列表值排序双列表、按周一至周日顺序排序日期字符串列表

Got it, let's tackle these two Kotlin sorting problems one by one!


1. Sort Two String Lists Based on the Order of the First List

I’ll cover two common scenarios for this requirement, since the exact use case can vary a bit:

Scenario 1: Keep elements paired while sorting the first list

If your two lists have one-to-one corresponding elements (e.g., list1 holds labels and list2 holds matching data), you can pair them up, sort by the first list’s values, then split back into separate lists:

fun main() {
    val labels = mutableListOf("b", "a", "c")
    val values = mutableListOf("2", "1", "3")

    // Pair elements from both lists to preserve their relationship
    val paired = labels.zip(values)
    
    // Sort the pairs using the first element (label) as the key
    val sortedPairs = paired.sortedBy { it.first }
    
    // Unzip back into two sorted lists
    val sortedLabels = sortedPairs.map { it.first }
    val sortedValues = sortedPairs.map { it.second }

    println("Sorted Labels: $sortedLabels") // Output: [a, b, c]
    println("Sorted Values: $sortedValues") // Output: [1, 2, 3]
}

Scenario 2: Sort using the first list as a predefined reference order

If the first list is a fixed priority order (e.g., a custom sequence you want to enforce), use its index to sort the second list (and adjust the first list if needed):

fun main() {
    val referenceOrder = listOf("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
    val scheduleItems = listOf("Sun 9am", "Mon 10am", "Wed 11am")

    // Sort scheduleItems based on their position in the referenceOrder list
    val sortedSchedule = scheduleItems.sortedBy { item ->
        // Extract the day abbreviation from the string (adjust this split logic if your format differs)
        val day = item.split(" ")[0]
        referenceOrder.indexOf(day)
    }

    println("Sorted Schedule: $sortedSchedule") // Output: [Mon 10am, Wed 11am, Sun 9am]
}

2. Sort Day-Time Strings by Monday-to-Sunday Order

For this task, we’ll create a custom order mapping for day abbreviations, then use that to sort your list. Note: I noticed you have "Web" in your list—assuming that’s a typo for "Wed", I’ve included support for both in the code:

fun main() {
    val scheduleList = listOf(
        "Sun 11:30 am - 9:00 pm",
        "Mon 11:30 am - 9:00 pm",
        "Tue 11:30 am - 9:00 pm",
        "Web 11:30 am - 9:00 pm", // Handling typo for "Wed"
        "Thu 11:30 am - 9:00 pm",
        "Fri 11:30 am - 9:30 pm",
        "Sat 11:30 am - 9:30 pm"
    )

    // Define our desired order: Monday → Sunday
    val dayPriorityMap = mapOf(
        "Mon" to 0,
        "Tue" to 1,
        "Wed" to 2,
        "Web" to 2, // Account for the typo
        "Thu" to 3,
        "Fri" to 4,
        "Sat" to 5,
        "Sun" to 6
    )

    // Sort the list using the priority map
    val sortedSchedule = scheduleList.sortedBy { item ->
        // Grab the day abbreviation from the start of the string
        val dayAbbrev = item.split(" ")[0]
        // Assign a high value to unknown days so they land at the end
        dayPriorityMap[dayAbbrev] ?: Int.MAX_VALUE
    }

    // Print the sorted result
    sortedSchedule.forEach { println(it) }
}

Output:

Mon 11:30 am - 9:00 pm
Tue 11:30 am - 9:00 pm
Web 11:30 am - 9:00 pm
Thu 11:30 am - 9:00 pm
Fri 11:30 am - 9:30 pm
Sat 11:30 am - 9:30 pm
Sun 11:30 am - 9:00 pm

The map assigns a numerical priority to each day, so sortedBy knows exactly how to order the items. Any unrecognized day abbreviations will get pushed to the end of the list.


内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 20:14:09