الفرق بين المراجعتين لصفحة: «Kotlin/collections/sortedWith»
< Kotlin | collections
أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: الدالة <code>sortedWith()</code> في لغة Kotlin}}</noinclude> تعيد الدالة<code>sortedWith()</code> Kotlin/Array|م...' |
لا ملخص تعديل |
||
| سطر 1: | سطر 1: | ||
<noinclude>{{DISPLAYTITLE: الدالة <code>sortedWith()</code> في لغة Kotlin}}</noinclude> | <noinclude>{{DISPLAYTITLE: الدالة <code>sortedWith()</code> في لغة Kotlin}}</noinclude> | ||
تعيد الدالة<code>sortedWith()</code> [[Kotlin/ | تعيد الدالة<code>sortedWith()</code> [[Kotlin/List|لائحة]] تضم عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/Iterable|المجموعة التكرارية]] التي استُدعيت عبرها مرتبة وفق المُقارِن المُمرّر <code>comparator</code>. | ||
==البنية العامة== | ==البنية العامة== | ||
<nowiki/><nowiki/><syntaxhighlight lang="kotlin"> | <nowiki/><nowiki/><syntaxhighlight lang="kotlin"> | ||
fun <T> Array<out T>. | fun <T> Array<out T>.sortedWith( | ||
comparator: Comparator<in T> | comparator: Comparator<in T> | ||
): | ): List<T> | ||
fun ByteArray.sortedWith( | |||
comparator: Comparator<in Byte> | |||
): List<Byte> | |||
fun ShortArray.sortedWith( | |||
comparator: Comparator<in Short> | |||
): List<Short> | |||
fun IntArray.sortedWith( | |||
comparator: Comparator<in Int> | |||
): List<Int> | |||
fun LongArray.sortedWith( | |||
comparator: Comparator<in Long> | |||
): List<Long> | |||
fun FloatArray.sortedWith( | |||
comparator: Comparator<in Float> | |||
): List<Float> | |||
fun DoubleArray.sortedWith( | |||
comparator: Comparator<in Double> | |||
): List<Double> | |||
fun BooleanArray.sortedWith( | |||
comparator: Comparator<in Boolean> | |||
): List<Boolean> | |||
fun CharArray.sortedWith( | |||
comparator: Comparator<in Char> | |||
): List<Char> | |||
fun <T> Iterable<T>.sortedWith( | |||
comparator: Comparator<in T> | |||
): List<T> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== القيمة المُعادة == | == القيمة المُعادة == | ||
[[Kotlin/ | [[Kotlin/List|لائحة]] تضم عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/Iterable|المجموعة التكرارية]] التي استُدعيت عبرها مرتبة وفق المُقارِن المُمرّر <code>comparator</code>. | ||
==أمثلة== | ==أمثلة== | ||
===استخدام الدالة <code>()sortedWith</code> مع المصفوفات=== | ===استخدام الدالة <code>()sortedWith</code> مع المصفوفات=== | ||
تعرف الشيفرة الآتية <nowiki/>[[Kotlin/Array|مصفوفة]] <nowiki/>باسم <code>array</code> مكونة من ستة عناصر باستخدام الدالة <code>()arrayOf</code>، ثم تستخدم الدالة<code>()sortedWith</code> على <code>array</code> لترتيبه وفق المقارِن | تعرف الشيفرة الآتية <nowiki/>[[Kotlin/Array|مصفوفة]] <nowiki/>باسم <code>array</code> مكونة من ستة عناصر باستخدام الدالة <code>()arrayOf</code>، ثم تستخدم الدالة<code>()sortedWith</code> على <code>array</code> لترتيبه وفق المقارِن المُمرّر، ثم تطبع الناتج:<syntaxhighlight lang="kotlin"> | ||
fun main(args: Array<String>) { | fun main(args: Array<String>) { | ||
val array = arrayOf(1, 9, -3, 7, 5, 6) | val array = arrayOf(1, 9, -3, 7, 5, 6) | ||
print ( | print (array.sortedWith(Comparator { a, b -> b - a })) // [9, 7, 6, 5, 1, -3] | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| سطر 32: | سطر 56: | ||
==مصادر== | ==مصادر== | ||
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sorted | *[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sorted-with.html الدالة sortedWith() في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.] | ||
[[تصنيف:Kotlin]] | [[تصنيف:Kotlin]] | ||
[[تصنيف:Kotlin Functions]] | [[تصنيف:Kotlin Functions]] | ||
مراجعة 19:20، 6 يونيو 2018
تعيد الدالةsortedWith() لائحة تضم عناصر المصفوفة أو المجموعة التكرارية التي استُدعيت عبرها مرتبة وفق المُقارِن المُمرّر comparator.
البنية العامة
fun <T> Array<out T>.sortedWith(
comparator: Comparator<in T>
): List<T>
fun ByteArray.sortedWith(
comparator: Comparator<in Byte>
): List<Byte>
fun ShortArray.sortedWith(
comparator: Comparator<in Short>
): List<Short>
fun IntArray.sortedWith(
comparator: Comparator<in Int>
): List<Int>
fun LongArray.sortedWith(
comparator: Comparator<in Long>
): List<Long>
fun FloatArray.sortedWith(
comparator: Comparator<in Float>
): List<Float>
fun DoubleArray.sortedWith(
comparator: Comparator<in Double>
): List<Double>
fun BooleanArray.sortedWith(
comparator: Comparator<in Boolean>
): List<Boolean>
fun CharArray.sortedWith(
comparator: Comparator<in Char>
): List<Char>
fun <T> Iterable<T>.sortedWith(
comparator: Comparator<in T>
): List<T>
القيمة المُعادة
لائحة تضم عناصر المصفوفة أو المجموعة التكرارية التي استُدعيت عبرها مرتبة وفق المُقارِن المُمرّر comparator.
أمثلة
استخدام الدالة ()sortedWith مع المصفوفات
تعرف الشيفرة الآتية مصفوفة باسم array مكونة من ستة عناصر باستخدام الدالة ()arrayOf، ثم تستخدم الدالة()sortedWith على array لترتيبه وفق المقارِن المُمرّر، ثم تطبع الناتج:
fun main(args: Array<String>) {
val array = arrayOf(1, 9, -3, 7, 5, 6)
print (array.sortedWith(Comparator { a, b -> b - a })) // [9, 7, 6, 5, 1, -3]
}
أنظر أيضًا
sortBy() : تقوم بترتيب المصفوفة أو اللائحة المتغيرة (MutableList) التي استُدعيت عبرها وفقًا للقيم التي تعيدها الدالة المُمرّرة .
sort() : تقوم بترتيب المصفوفة أو اللائحة المتغيرة (MutableList) التي استُدعيت عبرها.sorted() : تعيد لائحة تضم عناصر المصفوفة أو المجموعة التكرارية التي استُدعيت عبرها مرتبة وفق الترتيب الطبيعي.sortedBy() : تعيد لائحة تضم عناصر المصفوفة أو المجموعة التكرارية التي استُدعيت عبرها مرتبة ترتيبًا طبيعيًا وفقًا للقيم التي تعيدها الدالة المُمرّرةselector.