adding flex sorting code in draft form. needs integrating