Conditional sorting of columns in a matrix

I have a matrix with ten columns, and I want to rearrange these columns with three conditions.

  1. Column 1+2n and 2+2n (0<=n=<4) always remain together and in the same order (1+2n before 2+2n). Now we can say that we have five sets of columns, each having two columns.

  2. The 2+2n th columns should be sorted with respect to the first elements in them.

  3. In case first elements of any two 2+2n columns are same, they should be arranged with respect to the first elements of the corresponding 1+2n columns.

For example if the simplified matrix has only one row {1, 4, 7, 5, 8, 5, 0, 2, 3, 9}, it should be rearranged to {0, 2, 1, 4, 7, 5, 8, 5, 3, 9}.

Thanks for the help!

Sprite sorting is working fine but tilemap sorting is working the opposite way I want it to [duplicate]

I provided some screenshots to display what I’m talking about. The blue tile is a sprite, and the bushes are made from tilemap, each being 4 16×16 tiles. My transparency sort axis is set to 0,1,0. I’ve tried messing with the Z value in the axis as well to no avail. I’ll also point out that if I switch my sort axis to 0, -1, 0, the tile map sorting begins to work and the sprite sorting stops working. behind sprite

in front of sprite

in front of tilemap behind tilemap

WooCommerce products sorting by primary category first

I have a site with woocommerce and yoast seo plugin. I want to list the products on category archive page, by making the products of primary category (from yoast plugin) come first. Currently it does not do that. It is listing the products randomly or by menu order. The products have multiple categories assigned. For ex. the page of ‘Fashion’ category archive should list the products with primary category ‘Fashion’ to come first. Thanks

Combining multiple numeric meta_key’s in an order_by query for a custom woocommerce sorting option [closed]

I would like to create a custom woocommerce product orderby sort option that combines the values of one custom numeric field

  • ‘sort_price’

with another numeric field (woocommerce regular price)

  • ‘_price’

And order the combined values in ASC order

edit: I see this forum is wordpress only, I will take this to the woocommerce plugin forum, apologies, I can’t seem to delete this.

My code so far achieves ordering by sort_price alone but I am unsure how to combine the two.

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );  function custom_woocommerce_get_catalog_ordering_args( $  args ) {   $  orderby_value = isset( $  _GET['orderby'] ) ? wc_clean( $  _GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );     if ( 'random_list' == $  orderby_value ) {         $  args['orderby'] = 'meta_value_num';         $  args['order'] = 'ASC';         $  args['meta_key'] = 'sort_price';     }     return $  args; } add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' ); add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' ); function custom_woocommerce_catalog_orderby( $  sortby ) {     $  sortby['random_list'] = 'Price Low to High Custom';     return $  sortby; } 

there is an example in the wp codex which demonstrates using multiple meta keys but I am a little unsure of what the compare and value parts in the array are doing.

    'meta_query' => array(         'relation' => 'AND',         'state_clause' => array(             'key' => 'state',             'value' => 'Wisconsin',         ),         'city_clause' => array(             'key' => 'city',             'compare' => 'EXISTS',         ),      ),     'orderby' => array(          'city_clause' => 'ASC',         'state_clause' => 'DESC',     ), 

Merkle tree sorting leaves and pairs

I am implementing a Merkle tree and am considering using either of the two options.

The first one is sorting only by leaves. This one makes sense to me since you would like to have the same input every time you are constructing a tree from the data, that might not arrive sorted by default.

       CAB       /    \      CA     \    /    \    \   C     A     B /  \  /  \  /  \ 1  2  3  4  5  6 

The second one is sorting by leaves and pairs, which means that after sorting the leaves, you also sort all the pairs after hashing them, however I’m not entirely sure about the benefits of this implementation (if any).

       ACB       /    \      AC     \    /    \    \   C     A     B /  \  /  \  /  \ 1  2  3  4  5  6 

I have seen these implementations of Merkle trees in the past but am not sure about their benefits. So why choose one over the other?

Error in pivot selection algorithm for merge phase [Sorting]

In the paper Comparison Based Sorting for Systems with Multiple GPUs, the authors describe the selection of a pivot element with respect to the partition on the first GPU (and its mirrored counterpart on the other GPU-partition). That pivot element is crucial for being able to merge the two partitions, given that we have already sorted them on each GPU locally.

However, the pseudo-code for that pivot-selection, as shown in the paper, doesn’t seem to reflect the whole truth since when implementing it 1:1, the selected pivot element is off by some elements in some cases, depending on the input – the amount of elements to sort and therefore the amount of elements per partition (the chunk of data that each GPU gets).

To get more specific, the problem is – to my understanding – that the while loop is exited too early due to the stride being reduced down to zero before the correct pivot element has been found. In general, the approach is binary search-like, where the range of where the pivot can fall, is halved each iteration.

Can anyone spot what needs to be done here?

Here is a C++ implementation of the pivot selection:

size_t SelectPivot(const std::vector<int> &a, const std::vector<int> &b) {   size_t pivot = a.size() / 2;   size_t stride = pivot / 2;   while (stride > 0)   {     if (a[a.size() - pivot - 1] < b[pivot])     {       if (a[a.size() - pivot - 2] < b[pivot + 1] &&           a[a.size() - pivot] > b[pivot - 1])       {         return pivot;       }       else       {         pivot = pivot - stride;       }     }     else     {       pivot = pivot + stride;     }     stride = stride / 2;   }   return pivot; } 

P.S.: I tried ceiling the stride in order to not skip iterations when the stride is odd, but this introduced the issue of moving out of bounds of the array and even after handling those cases by clipping to the array bounds, the pivot was not always correct.

What factors of the integer dataset being sorted can I change, in order to compare two sorting algorithms?

I am comparing two comparison and binary data structure based sorting algorithms, the Tree Sort, and the Heap Sort. I am measuring the time taken for both algorithms to sort an increasing size of an integer dataset. However, I am wondering if there are any other variables which I can modify, for example standard deviation, in the integer dataset itself that would be of any benefit to my comparison.

Lower bound on comparison-based sorting

I have a question from one of the exercises in CLRS.

Show that there is no comparison sort whose running time is linear for at least half of the $ n!$ inputs of length $ n$ . What about a fraction of $ 1/n$ of the inputs of length $ n$ ? What about a fraction $ 1/2^n$ ?

I have arrived at the step where for a linear time sorter, there will we $ 2^n$ nodes in the decision tree, which is smaller than the $ n!$ leaves so this is a contradiction but I am unsure of how to formally write out the proof and extend it to the other fractions? The question also states that "for at least half of the $ n!$ inputs of length $ n$ ". I do not quite understand how it affects the number of leaves in the decision tree as any input of length $ n$ will have $ n!$ possible permutations.

What is the time complexity of sorting n words length wise and then alphabetically? Should we consider the length of the strings in the complexity?

Let’s assume I have a list of some words found in the English dictionary: ["hat", "assume", "prepare", "cat", "ball", "brave", "help" …. ]

I want to sort these words (which are n in number) in a way, such that they are ordered based on their length, but if 2 words have the same length, they are ordered alphabetically.

What is the time complexity of this sorting operation?

Would it be fair to say that the complexity is just O(nlogn) and not take into consideration the length of the strings? If the largest length is S, can the complexity also involve a factor of S?

Can’t reset tax query using ajax sorting plugin?

I have created an ajax sorting feature that filters a custom post type "Music" and its all separated by Genres and Tags.. the sorting works fine until.. I deselect all options. I’ve tried to add "all" as the default and added in the JS that if the genre is "all" to include all the posts. Every thing else works as desired but I need to be able to reset the tax query or include all.

Here is that javascript:

jQuery(document).ready(function($  ){                var bgenre = $  (":checkbox[name=bgenre]");     var btags = $  (":checkbox[name=btags]");          jQuery.merge( bgenre, btags ).on( "change", function() {              var genre;     var tags;            var genrearray = [];         var tagsarray = [];                           $  (":checkbox[name=bgenre]:checked").each(function() {              genrearray.push($  (this).val());                          //genre = genrearray.join();                 genre = genrearray;                  });                          $  (":checkbox[name=btags]:checked").each(function() {              tagsarray.push($  (this).val());              tags = tagsarray.join();                     });              //  var tags = tagsarray.join();          if (genrearray.length < 1) {         genre = 'all';     }          data = {             'action': 'filterlist',             'genre': genre,             'tags': tags                      };           $  .ajax({             url : ajaxurl,             data : data,             type : 'POST',             beforeSend : function ( xhr ) {                  $  ('.bplaylist').html( 'Loading...' );                 $  ('.js-Genre').attr( 'disabled', 'disabled' );                              },             success : function( data ) {                 if ( data ) {                                                        $  ('.bplaylist').html( data.posts );                       $  ('.js-Genre').removeAttr('disabled');                     $  ( '.js-Tags' ).removeAttr('disabled');                                      } else {                     $  ('.bplaylist').html( 'No posts found.' );                 }             }         });     })     }); 

and here is the php

function ajax_filterlist_handler() {                       //$  tags = esc_attr( $  _POST['tags'] );            $  genre = $  _POST['genre'];             $  tags = $  _POST['tags'];                                $  args = array(             'post_type' => 'download',             'post_status' => 'publish',             'posts_per_page' => -1,             'orderby' => 'date',             'order' => 'DESC',             'tax_query' => array(),             'relation' => IN         );                        if ( isset( $  _POST['genre']) && $  _POST['genre'] !== 'all'   ) {         $  args['tax_query'][] = array(             'taxonomy' => 'genre',             'field' => 'slug',             'terms' => $  genre          ); }           if ( isset( $  _POST['genre']) && $  _POST['genre'] === 'all'   ) {          $  args['tax_query'][] = array(             'taxonomy' => 'genre'                                         );                  }     if ( !isset( $  _POST['genre']) ) {          $  args['tax_query'][] = array(              );                  }                        if( isset( $  _POST['tags'] ) ) {         $  args['tax_query'][] = array(             'taxonomy' => 'download_tag',             'field' => 'slug',             'terms' => $  tags         );     }                 $  posts = 'No Posts Available';          $  the_query = new WP_Query( $  args );               if ( $  the_query->have_posts() ) :             ob_start();              while ( $  the_query->have_posts() ) : $  the_query->the_post();             get_template_part( '/custom/template-parts/trackitem' );             endwhile;              $  posts = ob_get_clean();         endif;          $  return = array(             'posts' => $  posts         );          wp_send_json($  return);     }     add_action( 'wp_ajax_filterlist', 'ajax_filterlist_handler' );     add_action( 'wp_ajax_nopriv_filterlist', 'ajax_filterlist_handler' );    

Sorry if the code is jumbled mess at this point. I was following a tutorial and after I got it working. I kinda went on my own to try to figure out the reset and/or all option.