Unraveling the Mechanics of Learning-Based Demonstration Selection for In-Context Learning