The answer to both questions is yes, and begins with how you obtain the two entries.
1 2 3 4 5
std::pair<int,int> a { 2, 20 };
int i_f = a.first;
int i_s = a.second;
This is a simple, old style means of gaining access to the first or second member of a pair.
1 2 3 4 5
std::pair<int,int> a { 2, 20 };
std::pair<int,int> b { 3, 30 };
int x = b.first - a.first;
Now, for sorting I'll leave some of the research up to you. What you need to find and learn is how to supply a custom sorting function to the sort algorithm. A tutorial is beyond the scope of a post here.
However, what I can tell you is that your function (or function object) which compares two entries will receive two const std::pair references (or should) from the source vector (or whatever container you're sorting), and you'll perform a comparison on, say, a.second - where a is the std::pair and you intend to sort on the second partner in the pair.
Now, about the "old style" I show above. Another, newer style (available in more recent C++ versions) is:
auto & [ i1, i2 ] = a;
This means that i1 and i2 will be references to the first and second parts of the pair, automatically typed to match the types in the pair.
i1 is the first, i2 is the second.
This is not available to you if your compiler is set to older versions of C++.