import groovy.json.JsonSlurper // import java.util.Random.getRandomDigits timeOutTime = 30000 apiUrl = "https://jsonplaceholder.typicode.com/todos/" def getArray(apiUrl){ def url = new URL(apiUrl) HttpURLConnection connection = (HttpURLConnection) url.openConnection() connection.setRequestMethod("GET") connection.setConnectTimeout(timeOutTime) try{ connection.connect() if (connection.responseCode == 200 || connection.responseCode == 201) { def data = connection.inputStream.text def slurper = new JsonSlurper() def result = slurper.parseText(data) return result } else { throw 'got wrong answer'; } }catch(e){ print e return null } } def sort(array){ int left = 0; int right = array.length; return mergeSort(array, left, right) } def mergeSort(array, left, rigth){ if (array.length <= 1 ) return array; int middle = (left + right )/2; mergeSort(array, left, middle); mergeSort(array, middle+1, right); return merge(array, left, middle, right); } def merge(array, left, middle, right){ int n1 = m - l + 1; int n2 = r - m; print right /* Create temp arrays */ def L = new int [n1]; def R = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i].id <= R[j].id) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } def map = [ [id:3],[id:10],[id:-2],[id:9],[id:4],[id:2],[id:19],[id:6]] map.sort { a,b -> a.id == b.id ? 0 : a.id > b.id ? -1 : 1 } println map // def array = getArray(apiUrl) // def list2 = [7, 4, -6, -1, 11, 2, 3, -9, 5, -13] // list2.sort { a, b -> a == b ? 0 : a > b ? -1 : 1 } // print list2
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more