الفرق بين المراجعتين ل"Kotlin/collections/sorted"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: الدالة <code>sorted()‎</code> في لغة Kotlin}}</noinclude> تقوم الدالة<code>sorted()‎</code> بترتيب Kotlin/Ar...')
 
سطر 1: سطر 1:
 
<noinclude>{{DISPLAYTITLE: الدالة <code>sorted()‎</code> في لغة Kotlin}}</noinclude>
 
<noinclude>{{DISPLAYTITLE: الدالة <code>sorted()‎</code> في لغة Kotlin}}</noinclude>
تقوم الدالة<code>sorted()‎</code> بترتيب  [[Kotlin/Array|المصفوفة]] أو [[kotlin/MutableList|اللائحة المتغيرة]] (<code>MutableList</code>) التي استُدعيت عبرها.  
+
تعيد الدالة<code>sorted()‎</code> [[Kotlin/List|لائحة]] تضم عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/Iterable|المجموعة التكرارية]] التي استُدعيت عبرها مرتبة وفق الترتيب الطبيعي.  
  
 
==البنية العامة==
 
==البنية العامة==
في حال استدعاء الدالة  <code>sort()‎</code> عبر  [[Kotlin/Array|مصفوفة]] فستقوم بترتيبها: <nowiki/><nowiki/><syntaxhighlight lang="kotlin">
+
<nowiki/><nowiki/><syntaxhighlight lang="kotlin">
fun IntArray.sort()
+
fun <T : Comparable<T>> Array<out T>.sorted(): List<T>  
fun LongArray.sort()
+
fun ByteArray.sorted(): List<Byte>
fun ByteArray.sort()
+
fun ShortArray.sorted(): List<Short>
fun ShortArray.sort()
+
fun IntArray.sorted(): List<Int>
fun DoubleArray.sort()
+
fun LongArray.sorted(): List<Long>
fun FloatArray.sort()
+
fun FloatArray.sorted(): List<Float>
fun CharArray.sort()
+
fun DoubleArray.sorted(): List<Double>
inline fun <T : Comparable<T>> Array<out T>.sort() (source)
+
fun CharArray.sorted(): List<Char>  
 
+
fun <T : Comparable<T>> Iterable<T>.sorted(): List<T>
</syntaxhighlight>في حال استدعاء الدالة  <code>sort()‎</code> عبر  [[Kotlin/Array|مصفوفة]] مع تمرير معاملين عددين فستقوم بترتيب المجال المحدد بذلك المعاملين داخل [[Kotlin/Array|المصفوفة]]: <nowiki/><nowiki/><syntaxhighlight lang="kotlin">
+
</syntaxhighlight>
fun <T> Array<out T>.sort(
 
    fromIndex: Int = 0,
 
    toIndex: Int = size)
 
fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size)
 
fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size)  
 
 
 
</syntaxhighlight>'''بيئة التشغيل: JVM'''
 
 
 
في حال استدعاء الدالة  <code>sort()‎</code> عبر  كائن من النوع <code>[[kotlin/MutableList|MutableList]]</code> فستقوم بترتيبه:<syntaxhighlight lang="kotlin">
 
fun <T : Comparable<T>> MutableList<T>.sort()  
 
 
 
</syntaxhighlight>في حال استدعاء الدالة  <code>sort()‎</code> عبر  [[Kotlin/Array|مصفوفة]] مع تمرير مًقارن <code>comparison</code> كمعامل فستقوم بترتيب تلك [[Kotlin/Array|المصفوفة]] بحسب الترتيب الذي يحدده المقارن <code>comparison</code>: <syntaxhighlight lang="kotlin">
 
inline fun <T> Array<out T>.sort(
 
    noinline comparison: (a: T, b: T) -> Int)
 
inline fun ByteArray.sort(
 
    noinline comparison: (a: Byte, b: Byte) -> Int)
 
inline fun ShortArray.sort(
 
    noinline comparison: (a: Short, b: Short) -> Int)
 
inline fun IntArray.sort(
 
    noinline comparison: (a: Int, b: Int) -> Int)
 
inline fun LongArray.sort(
 
    noinline comparison: (a: Long, b: Long) -> Int)
 
inline fun FloatArray.sort(
 
    noinline comparison: (a: Float, b: Float) -> Int)
 
inline fun DoubleArray.sort(
 
    noinline comparison: (a: Double, b: Double) -> Int)
 
inline fun CharArray.sort(
 
    noinline comparison: (a: Char, b: Char) -> Int)
 
  
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
+
== القيمة المُعادة ==
 +
[[Kotlin/List|لائحة]] تضم عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/Iterable|المجموعة التكرارية]] التي استُدعيت عبرها مرتبة وفق الترتيب الطبيعي.
  
 
==أمثلة==
 
==أمثلة==
 
===استخدام الدالة <code>()sorted</code> مع المصفوفات===
 
===استخدام الدالة <code>()sorted</code> مع المصفوفات===
 
تعرف الشيفرة الآتية  <nowiki/>[[Kotlin/Array|مصفوفة]]<nowiki/>باسم <code>array</code> مكونة من ستة عناصر باستخدام الدالة <code>()arrayOf‎</code>، ثم تستخدم الدالة<code>()sorted</code>  على <code>array</code> لترتيبه، ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 
تعرف الشيفرة الآتية  <nowiki/>[[Kotlin/Array|مصفوفة]]<nowiki/>باسم <code>array</code> مكونة من ستة عناصر باستخدام الدالة <code>()arrayOf‎</code>، ثم تستخدم الدالة<code>()sorted</code>  على <code>array</code> لترتيبه، ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
import java.util.Arrays
 
 
 
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)
 
+
   
array.sort()
 
 
      
 
      
print (Arrays.toString(array)) // [1, 3, 5, 6, 7, 9]
+
print (array.sorted()) // [-3, 1, 5, 6, 7, 9]ش
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
==أنظر أيضًا==
 
==أنظر أيضًا==
 
*<code>[[Kotlin/collections/sortBy|sortBy()]]</code>‎ : تقوم بترتيب  [[Kotlin/Array|المصفوفة]] أو [[Kotlin/MutableList|اللائحة المتغيرة]] (<code>MutableList</code>) التي استُدعيت عبرها وفقًا للقيم التي تعيدها الدالة المُمرّرة . 
 
*<code>[[Kotlin/collections/sortBy|sortBy()]]</code>‎ : تقوم بترتيب  [[Kotlin/Array|المصفوفة]] أو [[Kotlin/MutableList|اللائحة المتغيرة]] (<code>MutableList</code>) التي استُدعيت عبرها وفقًا للقيم التي تعيدها الدالة المُمرّرة . 
 +
 +
* <code>[[Kotlin/collections/sort|sort()]]</code>‎ : تقوم بترتيب <nowiki/>[[Kotlin/Array|المصفوفة]] أو اللائحة المتغيرة (<code>MutableList</code>) التي استُدعيت عبرها.
 +
 
==مصادر==
 
==مصادر==
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sort.html الدالة  sorted()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
+
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sorted.html الدالة  sorted()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin Functions]]
 
[[تصنيف:Kotlin Functions]]

مراجعة 18:25، 6 يونيو 2018

تعيد الدالةsorted()‎ لائحة تضم عناصر المصفوفة أو المجموعة التكرارية التي استُدعيت عبرها مرتبة وفق الترتيب الطبيعي.

البنية العامة

fun <T : Comparable<T>> Array<out T>.sorted(): List<T> 
fun ByteArray.sorted(): List<Byte> 
fun ShortArray.sorted(): List<Short> 
fun IntArray.sorted(): List<Int> 
fun LongArray.sorted(): List<Long> 
fun FloatArray.sorted(): List<Float> 
fun DoubleArray.sorted(): List<Double> 
fun CharArray.sorted(): List<Char> 
fun <T : Comparable<T>> Iterable<T>.sorted(): List<T>

القيمة المُعادة

لائحة تضم عناصر المصفوفة أو المجموعة التكرارية التي استُدعيت عبرها مرتبة وفق الترتيب الطبيعي.

أمثلة

استخدام الدالة ()sorted مع المصفوفات

تعرف الشيفرة الآتية  مصفوفةباسم array مكونة من ستة عناصر باستخدام الدالة ()arrayOf‎، ثم تستخدم الدالة()sorted  على array لترتيبه، ثم تطبع الناتج:

fun main(args: Array<String>) {
val array = arrayOf(1, 9, -3, 7, 5, 6)
    
    
print (array.sorted()) // [-3, 1, 5, 6, 7, 9]ش
}

أنظر أيضًا

  • sort()‎ : تقوم بترتيب المصفوفة أو اللائحة المتغيرة (MutableList) التي استُدعيت عبرها.

مصادر