logo
Tags down

shadow

Sort Specific Column of 2D int array (C++)


By : David Crosby
Date : October 15 2020, 08:10 PM
this one helps. Your sort function is very close to bubble sort, one of the simplest sorting algorithms to understand and implement. With a little modification, your code will work :
code :
    void sort2D(int arr[3][5], int rows, int columns) //the problem child
    {

            //input userCol ...

            //sorting start here ...
           bool found = true; //we can quit the loop if the array is already sorted.

            for (int bubble = 0; bubble < rows-1 && found ; bubble++)
            {
                    found = false;

                    for (int row_index=0; row_index < rows - bubble - 1; row_index++)
                    {
                            int temp;

                            if ((arr[row_index][userCol-1] < arr[row_index+1][userCol-1]))//if first value of selected column is less than next value
                            {
                                    //swap two elements.
                                    temp = arr[row_index+1][userCol-1];//create copy of second value
                                    arr[row_index+1][userCol-1]=arr[row_index][userCol-1]; //replace second value with first value
                                    arr[row_index][userCol-1]=temp;//set first equal to second's original value

                                    found = true; //we found something, continue to sort.
                            }
                    }
            }
            //print out the result ...
    }


Share : facebook icon twitter icon

sort mulitdimensional array in php according to specific column


By : lrnzzz
Date : March 29 2020, 07:55 AM
I wish did fix the issue. use usort for this
code :
function cmp($a, $b) {
    if ($a['second'] == $b['second']) {
        return 0;
    }
    return ($a['second'] < $b['second']) ? -1 : 1;
}
usort($array, 'cmp');
function cmp($a, $b) {
    if ($a['second'] == $b['second']) {
        if ($a['first'] == $b['first']) {
            return 0;
        }
        return ($a['first'] < $b['first']) ? -1 : 1;
    }
    return ($a['second'] < $b['second']) ? -1 : 1;
}
usort($array, "cmp");

Sort multiple type multidimension array by specific column


By : IrshadQureshi
Date : March 29 2020, 07:55 AM
will help you I have a multidimension String array. It contains numbers and strings. Is there any way to sort it by a specific integer column? e.g. I have this: , This solve your issue: (updated)
code :
public static void main(String[] args) {
    String[][] temp = {
        { "Fox", "32", "One" },
        { "Dog", "45", "Two" },
        { "Cat", "34", "Three" },
        { "Snake", "3", "Four" }
    };

    Arrays.sort(temp, new Comparator<String[]>() {
        @Override
        public int compare(String[] row1, String[] row2) {
            return Integer.compare(
                    Integer.parseInt(row1[1]), Integer.parseInt(row2[1]));
        }
    });

    System.out.println(Arrays.deepToString(temp));
}

How to sort a numpy 2D array rows descending or ascending using a specific column indices


By : Jordi Puigsegur Figu
Date : March 29 2020, 07:55 AM
hop of those help? Here is how you can do it using the numpy_indexed package:
code :
import numpy_indexed as npi
print(a[npi.argsort((a[:,1], -a[:,0]))])

sort a numpy array (having strings, dictionaries and numbers ) with respect to specific column


By : Ibiã Marques
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have the following numpy array (having strings, dictionaries and numbers ) and would like to sort this array according to the last column (number) , This could be one way using the last column X[-1] as the sorting key
code :
import numpy as np

X=np.array([['few', {'age': 'young'}, {'salary': 'low'}, 0.8],
['few', {'salary': 'low'}, {'age': 'young'}, 0.3],
['few', {'age': 'young'}, {'salary': 'medium'}, 0],
['most', {'salary': 'high'}, {'education': 'upper'}, 1]])

X = np.array(sorted(X, key=lambda X: X[-1]) )
# array([['few', {'age': 'young'}, {'salary': 'medium'}, 0],
#        ['few', {'salary': 'low'}, {'age': 'young'}, 0.3],
#        ['few', {'age': 'young'}, {'salary': 'low'}, 0.8],
#        ['most', {'salary': 'high'}, {'education': 'upper'}, 1]],
#       dtype=object)
from operator import itemgetter

X = sorted(X, key=itemgetter(-1))

How to sort array of type Object[][] by a specific column


By : Mathias Bille Nielse
Date : March 29 2020, 07:55 AM
will be helpful for those in need I am writing some java code for my project and I came across this issue that I do not know how to deal with. I have a two-dimensional array like this: , Try this:
code :
Object[][] array = new Object[10][3];
// fill the array

Object[][] sortedArray = Arrays.stream(array)
        .filter(v -> v[2] != null) //not required if you have the array completely filled
        .sorted(Comparator.comparingDouble(v -> (double) v[2]))
        .toArray(Object[][]::new);

System.out.println(Arrays.deepToString(sortedArray));
Related Posts Related Posts :
  • Malloc of pointer to an array- C++
  • Changing audio stream from physical input
  • Sorting a Vector of a custom class with std::sort() causes a segmentation fault
  • fastest way to read the last line of a string?
  • std::num_put issue with nan-boxing due to auto-cast from float to double
  • Difficulty with map's find function
  • Visual C++ executable will not run without Boost DLLs
  • #include <mutex> causes the bind() function call to throw errors at compile time, why?
  • Random_shuffle alternative in C++Builder6
  • C++ unique_ptr initialization
  • Is it really safe not to check in the copy assignemt operator whether an object is assigned to itself?
  • How to avoid passing param to template instantiation of reference-based helper class
  • c++ program not printing 2d vector grid properly but is close
  • Why isn't my array incrementing properly?
  • How to create your own Function for Async operation in c++ UWP?
  • C++ accessing a variable in a union struct
  • What does a implicitly defined destructor do
  • Member was not declared in base class template
  • Overloading << sign follow by a boolean expression
  • looser throw specifier for virtual function : GCC 4.9 complains but MSVC
  • Why does decltype(auto) not work as expected?
  • How to print and read at in the same line in Haskell?
  • SFINAE with Templated class Member functions
  • how to declare char * with input of {"data": "0001"}'
  • c++ program compile with LAPACK library
  • Enum in C++ Classes
  • Unordered_map find const wchar_t*
  • OK to use these two parameters each independently, but fail to have both
  • Two classes with same, standalone, hidden functionality
  • C++: Instantiate object with no namespace
  • qml set text property from c++
  • Confused Backtrace of GDB while analysing core dump file at ../stdlib/strtod_l.c:734
  • syscall.MustLoadDll.MustFindProc throws "The specified procedure could not be found"
  • Why does structured binding not work as expected on struct?
  • Using lambda expression as Compare for std::set, when it's inside a vector
  • C++ Can't reference key of the map
  • Generic QuickSort implemented with vector and iterators C++
  • purpose for wait_for function in condition variable - C++11
  • Can I fill a template parameter with a nested class in this class?
  • Inconsistency parsing numeric literals according to C++ Standard's grammar
  • Array with fixed size at runtime
  • Which is better to check if a character exists in a std::string? find or find_first_of?
  • Insert element (TinyXml)
  • My argv[] in main always returns 0 when converted to integer
  • Indirect Path to Files, when program is called from somewhere else
  • Find all reachable vertices in a Boost BGL graph efficiently
  • Paint context and tool in maya for custom blendshape
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org