Sort File Snippet
#1
Sort File Snippet


Introduction

This is a very simple executable method that will:
  1. Open a text file
  2. Sort the lines alphabetically
  3. Re-write the txt file with the sorted lines



Screenshots

This sorts a file... what would you see???



Script

Code:
#==============================================================================
# ** Sort File Snippet
#------------------------------------------------------------------------------
#  This is a very simple executable method that will:
#    1) Open a text file
#    2) Sort the lines alphabetically
#    3) Re-write the txt file with the sorted lines
#==============================================================================


#--------------------------------------------------------------------------
# * Sort a file
#--------------------------------------------------------------------------
#
def sort_a_file
  #
  # Part 1: Define the file (in the project's root directory)
  filename  = "supercalifragilistic_expialidocious.txt"
  #
  # Part 2: Sort the contents
  file_handle = File.new(filename)        # Create a File's I/O Source variable
  
    # ---------------------------------------------------------------------
    #                                     # Optionally, an I/O Source variable
    #                                     # may have been opened with the 'r'
    #                                     # flag meaning 'readable'
    #
    # file_handle = File.open(filename, 'r')
    # ---------------------------------------------------------------------
    
  lines       = file_handle.readlines     # Read all lines from the I/O source
  file_handle.close                       # Close the I/O source
  #
  # Part 3: Sort the contents
  lines.sort!                             # Sort all the lines read
  #
  # Part 4: Write the contents back
  file_handle = File.open(filename, 'w')  # Set a I/O Source as a writeable file
  file_handle.write(lines)                # Write all lines into the I/O Source
  file_handle.close                       # Close the I/O source
  #
end
  
# Execute the method at start
sort_a_file


Instructions

This is more of a simple feature and/or a tool, and not really used as a game system in itself. Comically, I've used it to sort through massive lists I've accumulated.



FAQ

A lot of the file size is from comments explaining what each line generally performs.



Compatibility

Given it is nothing but RUBY, it isn't just an RMXP script but should work cross-Ruby-platform.



Terms and Conditions

No credit required. Its too simple.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)

[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png]    [Image: liM4ikn.png]    [Image: fdzKgZA.png]    [Image: sj0H81z.png]
[Image: QL7oRau.png]    [Image: uSqjY09.png]    [Image: GAA3qE9.png]    [Image: 2Hmnx1G.png]    [Image: BwtNdKw.png%5B]
  Above are clickable links
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
   Moghunter Menus: Scene File Ayumi DerVVulfman 4 15,138 07-28-2016, 05:13 PM
Last Post: Djigit
   File Missing Error Preventer xnadvance 0 6,184 05-29-2011, 05:24 AM
Last Post: xnadvance
   Variable Swap [Snippet] PK8 0 5,027 11-09-2009, 07:32 AM
Last Post: PK8
   Switch Swap [Snippet] PK8 0 4,908 11-09-2009, 07:07 AM
Last Post: PK8
   Audio File Encryption Dark-Yen 0 5,584 03-08-2008, 06:35 AM
Last Post: Dark-Yen
   Load file error dialog Zeriab 0 4,829 03-06-2008, 06:00 AM
Last Post: Zeriab
   Loading Data from File Trickster 0 5,488 03-02-2008, 06:04 AM
Last Post: Trickster



Users browsing this thread: 1 Guest(s)