How do I get the index for a custom post?

For example if there are 10 custom posts, and No. 1 is the oldest, and
they happened to select the 3rd oldest one, they want it to say “No.
3”.

There is a more elegant way than that function:

$args = array(
  'post_type' => 'book',
  'fields' => 'ids',
  'ignore_sticky_posts' => true,
  'orderby' => 'post_date',
  'order' => 'DESC'
);
$posts = new WP_Query($args);
// var_dump($posts->posts);

$index = array_search(300,$posts->posts);
if (!empty($index)) {
  echo $index + 1;
} else {
  echo 'not found';
}

It is hardly worth a function, really, but if you want one:

function get_post_index ( $ids, $vs = 0 {
  if (empty($ids) || !is_array($ids)) return false;
  $index = array_search($vs,$ids);
  if (!empty($index)) {
    return $index + 1;
  } else {
    return false;
  }
}