Postgresql Plpgsql Loop Through Json Javascript Array Sort By Property