added `sortByMid` fn and tests