[Solved] Matrix sorting in C

#include <stdio.h> #include <stdlib.h> typedef int(*comparer) (int a, int b); int compareasc ( const void *pa, const void *pb ) { const int *a = *(const int **)pa; const int *b = *(const int **)pb; if(a[0] == b[0]) return a[1] – b[1]; else return a[0] – b[0]; } int comparedsc ( const void *pa, const … Read more

[Solved] How to sort numbers in C# XML?

var xml=@” <highscore> <score> <naam>Pipo</naam> <punten>200</punten> </score> <score> <naam>Harry</naam> <punten>400</punten> </score> </highscore>”; var doc = XDocument.Parse(xml); var orderedScoreElements = doc.Root .Elements(“score”) .OrderByDescending(e => (int)e.Element(“punten”)) .ToList(); and to rewrite the doc in order: doc.Root.RemoveNodes(); doc.Root.Add(orderedScoreElements); 1 solved How to sort numbers in C# XML?

[Solved] How to sort/order tuple of ip address in python

You can do it like this: a = [{‘host’: u’10.219.1.1′}, {‘host’: u’10.91.1.1′}, {‘host’: u’10.219.4.1′}, {‘host’: : ‘10.91.4.1’}] sorted(a, key=lambda x: tuple(int(i) for i in x[‘host’].split(‘.’))) # [{‘host’: ‘10.91.1.1’}, {‘host’: ‘10.91.4.1’}, {‘host’: ‘10.219.1.1’}, {‘host’: ‘10.219.4.1’}] sorted(a, key=lambda x: tuple(int(i) for i in x[‘host’].split(‘.’))[::-1]) # [{‘host’: ‘10.91.1.1’}, {‘host’: ‘10.219.1.1’}, {‘host’: ‘10.91.4.1’}, {‘host’: ‘10.219.4.1’}] 2 solved How to … Read more

[Solved] Java sort 4 arrays into 1 array

Step 1: Combine all the arrays Step 2: Sort arrays using Arrays.sort(array); Step 3: Remove the duplicates. int[] a = {1,2,3,4,5}; int[] b = {1,2,3,4,5,6}; int[] c = {1,3,7}; int[] d = {2,3,4,8,9,10}; int[] resultArray1 = new int[a.length+b.length+c.length+d.length]; int arrayIndex = 0; for (int i=0; i< a.length ; i++, arrayIndex++ ) { resultArray1[arrayIndex] = a[i]; … Read more

[Solved] Sort out array of object using its value [duplicate]

Use String.localeCompare() with the numeric option to sort using possibility, but use – (minus) to get descending results. If the comparison returns 0 (equals), use localeCompare again to compare the names: const array = [{“name”:”fever”,”possibility”:”20%”},{“name”:”hiv”,”possibility”:”25%”},{“name”:”heart-attack”,”possibility”:”20%”},{“name”:”covid”,”possibility”:”40%”}] const result = array.sort((a, b) => -a.possibility.localeCompare(b.possibility, undefined, { numeric: true }) || a.name.localeCompare(b.name) ) console.log(result) solved Sort out array … Read more

[Solved] Why does looking up an index *before* a swap rather than inline change the result?

Let’s add some tracing so we can see order-of-operations: import sys def findIdx(ary, tgt): retval = ary.index(tgt) print(‘First instance of %r in %r is at position %r’ % (tgt, ary, retval), file=sys.stderr) return retval data1 = [1.48, -4.96] i = 0 mn = data1[1] k = findIdx(data1, mn) data1[i], data1[k] = data1[k], data1[i] print(“Prior lookup: … Read more

[Solved] How to create various types of inputs in order to test my algorithm? [closed]

You can use Arrays.sort() to sort your array before feeding it, each of the following describe how to achieve one thing you asked for – try to do each on the array (one at a time) before invoking sort(a) To sort the array in ascending order: Arrays.sort(a); To sort the array in descending order: Arrays.sort(a,Collections.reverseOrder()); … Read more

[Solved] Compare inline function in std::sort()

Read more about C++11 at least (or C++14 or C++17), e.g. some good C++ programming book (any standard older than C++11 is obsolete, and C++11 has evolved a lot since its predecessors, so you should almost consider C++11 as a new programming language). Look also some C++ reference site. [](const string &left, const string &right) … Read more

[Solved] Creating numbered list of output

You’d still use enumerate(); you didn’t show how you used it but it but it solves your issue: for index, (value,num) in enumerate(sorted_list, start=1): print(“{}.\t{:<5}\t{:>5}”.format(index, value,num)) I folded your str.ljust() and str.rjust() calls into the str.format() template; this has the added advantage that it’ll work for any value you can format, not just strings. Demo: … Read more

[Solved] arranging numbers in ascending order [closed]

This is quite basic program called selection sort. The wiki article is: Selection sort. Basically in this program, i is first pointing to the first element in the array. Then, j points to the next element. If, the element j is smaller than element i, they get swapped. After swapping, the inner for loop still … Read more

[Solved] Sorting arrays manually in php without using sort() [closed]

here is the solution using bubble sort <?php $item = array(2, 1, 4,3,5,6); $item_length = count($item); for ($counter = 0; $counter < $item_length-1; $counter++) { for ($counter1 = 0; $counter1 < $item_length-1; $counter1++) { if ($item[$counter1] > $item[$counter1 + 1]) { $temp=$item[$counter1]; $item[$counter1]=$item[$counter1+1]; $item[$counter1+1]=$temp; } } } //you can print the array using loop print_r($item); … Read more