TensorFlow
array sorting
machine learning
data preprocessing
Python programming

Sorting an Array in TensorFlow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

TensorFlow can sort tensors directly, so you usually do not need to convert data back to NumPy just to order values. The core tools are tf.sort for sorted values and tf.argsort for sorted indices. Which one you use depends on whether you need the reordered values themselves or the index positions that define the order.

Sort Values With tf.sort

The most direct operation is tf.sort, which returns a tensor with the same shape as the input but with elements sorted along a chosen axis.

python
1import tensorflow as tf
2
3values = tf.constant([4, 1, 9, 2, 5])
4print(tf.sort(values))
5print(tf.sort(values, direction="DESCENDING"))

By default, TensorFlow sorts along the last axis in ascending order. You can change the axis and direction explicitly.

python
1matrix = tf.constant([
2    [3, 1, 2],
3    [9, 7, 8],
4])
5
6print(tf.sort(matrix, axis=1))
7print(tf.sort(matrix, axis=0))

Sorting by axis=1 sorts each row independently. Sorting by axis=0 sorts each column independently.

Use tf.argsort When You Need the Order Indices

Often the real goal is to reorder another tensor according to the sort order. In that case, use tf.argsort.

python
scores = tf.constant([0.2, 0.9, 0.1])
order = tf.argsort(scores, direction="DESCENDING")
print(order)

That gives the indices of the sorted order, not the sorted values.

You can use those indices with tf.gather to reorder related values.

python
labels = tf.constant(["low", "high", "very low"])
sorted_labels = tf.gather(labels, order)
print(sorted_labels)

This is the standard ranking pattern when scores and payload data need to stay aligned.

Sort Batched Tensors

In machine-learning code, you often sort each batch row independently.

python
1batch_scores = tf.constant([
2    [0.3, 0.1, 0.9],
3    [0.5, 0.4, 0.8],
4])
5
6order = tf.argsort(batch_scores, axis=1, direction="DESCENDING")
7print(order)

If you want to reorder a companion tensor using those per-row indices, combine tf.argsort with tf.gather(..., batch_dims=1).

python
1items = tf.constant([
2    [101, 102, 103],
3    [201, 202, 203],
4])
5
6sorted_items = tf.gather(items, order, axis=1, batch_dims=1)
7print(sorted_items)

This is a common pattern in recommendation and ranking models.

Use top_k When Full Sorting Is Unnecessary

If you only need the top few elements, tf.math.top_k is often better than sorting everything.

python
values, indices = tf.math.top_k(tf.constant([4.0, 1.0, 9.0, 2.0]), k=2)
print(values)
print(indices)

That avoids unnecessary work when you care only about the best k results.

TensorFlow Versus NumPy

If the data is already a TensorFlow tensor and you want to keep the computation in the TensorFlow graph or on the device, use TensorFlow sorting ops. Converting to NumPy just to sort may break gradient flow, add copies, and move data off device.

If the code is purely offline preprocessing and already lives in NumPy or pandas, then NumPy sorting may be simpler. The right answer depends on where the data already lives.

Common Pitfalls

The most common mistake is confusing tf.sort and tf.argsort. One returns sorted values, the other returns the index order.

Another issue is forgetting the axis. TensorFlow sorts along the last axis by default, which may not match your tensor layout.

Developers also sometimes sort scores but forget to apply the same order to the related labels or payload tensors. That breaks alignment silently.

Finally, if you need only a few top results, do not full-sort by habit. tf.math.top_k is usually the better fit.

Summary

  • Use tf.sort when you want sorted values.
  • Use tf.argsort when you need the order indices.
  • Set the axis explicitly when tensor layout matters.
  • Use tf.gather with sorted indices to keep related tensors aligned.
  • Use tf.math.top_k instead of full sorting when you need only the best few items.

Course illustration
Course illustration

All Rights Reserved.