<?php

add_filter( 'mce_buttons', function( $originalButtonsInFirstRow ){
    $buttonsToAdd = [
      //'new_button_ID_to_add' => 'put_it_after_button_ID',
      'alignjustify'  => 'alignright', //add justify after align right
      'underline'     => 'italic', //add underline after italic
    ];
    $newButtonsInFirstRow = [];
    foreach( $originalButtonsInFirstRow as $buttonID ){
        $newButtonsInFirstRow[] = $buttonID; //re-push originals to keep them intact
        
        foreach( $buttonsToAdd as $newButtonID => $putItAfterButtonID ){
        	//Firstly, chack whether the button was already added by us or not! (...!==true)
            if( ($putItAfterButtonID !== true) && ($buttonID == $putItAfterButtonID) ){
              $newButtonsInFirstRow[] = $newButtonID;
              $buttonsToAdd[$newButtonID] = true; //make a note that button was added
            }
        }
    }
    
    //Now check, if all buttons where actually added (in case "put_it_after_button_ID" was missing...)
    foreach( $buttonsToAdd as $newButtonID => $putItAfterButtonID ){
        // check if the button was added (note the type check !== (negated ===))
        //if the button wasn't added, put it at the and of the buttons array
        if( $putItAfterButtonID !== true ) 
          $newButtonsInFirstRow[] = $newButtonID; 
    }
    
    
    return $newButtonsInFirstRow;  
}, 5 );