wp rest api orderby field in a custom table

This is the solution in the mentioned link if we want to use meta value to order the rest API content:

// Add meta your meta field to the allowed values of the REST API orderby parameter
add_filter(
    'rest_' . $post_type . '_collection_params',
    function( $params ) {
        $params['orderby']['enum'][] = 'YOUR_META_KEY';
        return $params;
    },
    10,
    1
);

// Manipulate query
add_filter(
    'rest_' . $post_type . '_query',
    function ( $args, $request ) {
        $order_by = $request->get_param( 'orderby' );
        if ( isset( $order_by ) && 'YOUR_META_KEY' === $order_by ) {
            $args['meta_key'] = $order_by;
            $args['orderby']  = 'meta_value'; // user 'meta_value_num' for numerical fields
        }
        return $args;
    },
    10,
    2
);

But I want to order the rest API contents by a field that’s not in the meta table.

the table structure:

id views
-- -----
1   5
2   2
3   20
4   1

I want to sort the rest API posts descending by views in this table.
to do this I’ve edited Manipulate query section so I’ve created an array that has ids sorted by views. (to do the sort I’ve used the solution in this post)
then I’ve used post__in in args with the array that I’ve created before as its parameter and then I’ve used post__in as orderby parameter:

// Manipulate query

add_filter(
    'rest_' . $post_type . '_query',
    function ( $args, $request ) {
        //create an array that have ids that are sorted by views
        global $wpdb;   
        $post_views = $wpdb->get_results( $wpdb->prepare("SELECT id, views FROM " . $wpdb->prefix . "mytable") );
        $views = array_column($post_views, 'views');
        array_multisort($views, SORT_DESC, $post_views);
        $most_viewd = array_column($post_views, 'id');  
    
        $order_by = $request->get_param( 'orderby' );
        if ( isset( $order_by ) && 'views' === $order_by ) {
            $args['post__in'] = $most_viewd;
            $args['orderby']  = 'post__in';
        }
        return $args;
    },
    10,
    2
);