There is a certain card file of people:
The essence of the card file is to store such cards here, each of which contains three names initially. Each card is numbered. The surnames in the cards and the card file are generally arranged in the sort order by alphabet.
In MySQL, the table storing this file looks like this:
- id
- card_number (card number)
- position (position of the last name in the card)
- surname (last name)
Task: add a new surname to the card file with saving the positions by the surname that are already there, but in the nearest sorting card.
For example, we need to add the surname "Ivaschenko". The surname Ivashchenko should be after Ivanov and before Ilitayev, according to the sorting rules, but since we cannot change the order of the names that already exist in the card, we should just add it to the end of card No. 1, after the last name "Ilitaev"
As a result, I need to somehow determine the number of the card in which I am going to add this name for a given last name (for example, for Ivashchenko No. 1).

Ирсеньеваwhere to add to the first or second. And initially there are cards on all letters. What if you didn’t initially have 3 surnames for a rare letter, will you get a card and come up with a third surname for it from the ceiling? - Mike