Thanks a lot .. This loop worked after I initialized the OLDKEY variable.
But I am still not clear how the code is working and getting the result. I believe I understood the part where you are storing the key value in to $KEY on a -r result .. But not clear how the latest file is picked based on the seconds in the file.
It will be of great help if you can explain how the code is working.
The files are sorted in reverse order; that why the "command substitution" for ls needs to be used. So per key, the latest version is the first one. For a new key, the first line is printed as OLDKEY still holds the old key, and key is stored in OLDKEY for further reference.