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

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

مراجعة 13:52، 4 يونيو 2018

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

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

في حال استدعاء الدالة  slice()‎ عبر مصفوفة فستقوم بترتيبها:

fun IntArray.sort() 
fun LongArray.sort() 
fun ByteArray.sort() 
fun ShortArray.sort() 
fun DoubleArray.sort() 
fun FloatArray.sort() 
fun CharArray.sort() 
inline fun <T : Comparable<T>> Array<out T>.sort() (source)

في حال استدعاء الدالة  slice()‎ عبر مصفوفة مع تمرير معاملين عددين فستقوم بترتيب المجال المحدد بذلك المعاملين داخل المصفوفة:

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)

بيئة التشغيل: JVM في حال استدعاء الدالة  slice()‎ عبر كائن من النوع MutableList فستقوم بترتيبه:

fun <T : Comparable<T>> MutableList<T>.sort()

في حال استدعاء الدالة  slice()‎ عبر مصفوفة مع تمرير مًقارن comparison كمعامل فستقوم بترتيب تلك المصفوفة بحسب الترتيب الذي يحدده المقارن comparison:

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)

يُلاحَظ وجود الكلمة المفتاحية inline للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع توثيق الدوال المباشرة (inline functions).

أمثلة

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

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

import java.util.Arrays

fun main(args: Array<String>) {
val array = arrayOf(1, 9, 3, 7, 5, 6)

array.sort()
    
print (Arrays.toString(array)) // [1, 3, 5, 6, 7, 9]
}

أنظر أيضًا

مصادر