code: def sort_ltr_samples(
      X: sparse.csr_matrix,
      y: npt.NDArray[np.int32],
      qid: npt.NDArray[np.int32],
      clicks: npt.NDArray[np.int32],
      pos: npt.NDArray[np.int64],
  ) -> Tuple[
      sparse.csr_matrix,
      npt.NDArray[np.int32],
      npt.NDArray[np.int32],
      npt.NDArray[np.int32],
  ]:
      """Sort data based on query index and position."""
      sorted_idx = np.argsort(qid)
      X = X[sorted_idx]
      clicks = clicks[sorted_idx]
      qid = qid[sorted_idx]
      pos = pos[sorted_idx]
  
      indptr, _, _ = rlencode(qid)
  
      for i in range(1, indptr.size):
          beg = indptr[i - 1]
          end = indptr[i]
  
          assert beg < end, (beg, end)
          assert np.unique(qid[beg:end]).size == 1, (beg, end)
  
          query_pos = pos[beg:end]
          assert query_pos.min() == 0, query_pos.min()
          assert query_pos.max() >= query_pos.size - 1, (
              query_pos.max(),
              query_pos.size,
              i,
              np.unique(qid[beg:end]),
          )
          sorted_idx = np.argsort(query_pos)
  
          X[beg:end] = X[beg:end][sorted_idx]
          clicks[beg:end] = clicks[beg:end][sorted_idx]
          y[beg:end] = y[beg:end][sorted_idx]
          # not necessary
          qid[beg:end] = qid[beg:end][sorted_idx]
  
      data = X, clicks, y, qid
  
      return data
  
imports: import os
  import zipfile
  from dataclasses import dataclass
  from typing import TYPE_CHECKING, Any, Callable, Dict, Generator, NamedTuple, Optional, Tuple, Type, Union
  from urllib import request
  import numpy
  import pytest
  from numpy import typing
  from numpy.random import Generator
  from scipy import sparse
  import xgboost
  from xgboost.data import pandas_pyarrow_mapper
label_desc: Use image node_id 15 for calling a global function with example usage: sort_ltr_samples(X, y, qid, clicks, pos) and returns: data